Ejemplo n.º 1
0
  @Override
  protected DbUnitDatabaseConnection createDbUnitConnection(String schemaName) {
    Assert.notNull(schemaName, "数据集的模式名不能为空,且必须规范。");
    // 获取数据连接
    DbUnitDatabaseConnection connection = super.createDbUnitConnection(schemaName);

    // 没有根据数据库类型使用特订数据源的MetadataHandler,以下代码防止取不到表的元数据信息
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    boolean isMySQL = false;
    try {
      String databaseProductName =
          connection.getConnection().getMetaData().getDatabaseProductName();
      if ("MySQL".equals(databaseProductName)) {
        isMySQL = true;
        logger.info(
            "Database product name = "
                + databaseProductName
                + ", set DatabaseConfig.PROPERTY_METADATA_HANDLER to new MySqlMetadataHandler()");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    // if database is MySQL, reset PROPERTY_METADATA_HANDLER to use MySQL specific Handler
    if (isMySQL) {
      DatabaseConfig config = connection.getConfig();
      config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    return connection;
  }
Ejemplo n.º 2
0
  /**
   * Creates a new instance of dbUnit's <code>IDatabaseConnection</code>
   *
   * @param schemaName The schema name, not null
   * @return A new instance of dbUnit's <code>IDatabaseConnection</code>
   */
  protected DbUnitDatabaseConnection createDbUnitConnection(String schemaName) {
    // A DbSupport instance is fetched in order to get the schema name in correct case
    DataSource dataSource = getDatabaseModule().getDataSourceAndActivateTransactionIfNeeded();
    SQLHandler sqlHandler = new DefaultSQLHandler(dataSource);
    DbSupport dbSupport = getDbSupport(configuration, sqlHandler, schemaName);

    // Create connection
    DbUnitDatabaseConnection connection =
        new DbUnitDatabaseConnection(dataSource, dbSupport.getSchemaName());
    DatabaseConfig config = connection.getConfig();
    if (dbSupport.getDatabaseDialect().toLowerCase().equals("mysql")) {
      config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
      config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());
    }

    // Make sure that dbunit's correct IDataTypeFactory, that handles dbms specific data type
    // issues, is used
    IDataTypeFactory dataTypeFactory =
        getInstanceOf(IDataTypeFactory.class, configuration, dbSupport.getDatabaseDialect());
    config.setProperty(PROPERTY_DATATYPE_FACTORY, dataTypeFactory);
    // Make sure that table and column names are escaped using the dbms-specific identifier quote
    // string
    config.setProperty(
        PROPERTY_ESCAPE_PATTERN,
        dbSupport.getIdentifierQuoteString() + '?' + dbSupport.getIdentifierQuoteString());
    // Make sure that batched statements are used to insert the data into the database
    config.setProperty(FEATURE_BATCHED_STATEMENTS, "true");
    // Make sure that Oracle's recycled tables (BIN$) are ignored (value is used to ensure
    // dbunit-2.2 compliancy)
    config.setProperty("http://www.dbunit.org/features/skipOracleRecycleBinTables", "true");

    return connection;
  }
Ejemplo n.º 3
0
 /**
  * Closes (i.e. return to the pool) the JDBC Connection that is currently in use by the
  * DbUnitDatabaseConnection
  */
 protected void closeJdbcConnection() {
   try {
     for (DbUnitDatabaseConnection dbUnitDatabaseConnection : dbUnitDatabaseConnections.values()) {
       dbUnitDatabaseConnection.closeJdbcConnection();
     }
   } catch (SQLException e) {
     throw new UnitilsException("Error while closing connection.", e);
   }
 }
Ejemplo n.º 4
0
 /**
  * Gets the DbUnit connection or creates one if it does not exist yet.
  *
  * @param schemaName The schema name, not null
  * @return The DbUnit connection, not null
  */
 public DbUnitDatabaseConnection getDbUnitDatabaseConnection(String schemaName) {
   DbUnitDatabaseConnection dbUnitDatabaseConnection = dbUnitDatabaseConnections.get(schemaName);
   if (dbUnitDatabaseConnection == null) {
     dbUnitDatabaseConnection = createDbUnitConnection(schemaName);
     DatabaseConfig config = dbUnitDatabaseConnection.getConfig();
     dbUnitDatabaseConnections.put(schemaName, dbUnitDatabaseConnection);
   }
   return dbUnitDatabaseConnection;
 }