Search in sources :

Example 1 with HandlingDataSourceWrapper

use of org.dbflute.jdbc.HandlingDataSourceWrapper in project dbflute-core by dbflute.

the class DfSchemaXmlSerializer method countDownRaceProcessTable.

protected void countDownRaceProcessTable(final List<DfTableMeta> tableList, int runnerCount, final DfFittingDataSource fittingDs) {
    final CountDownRace fireMan = new CountDownRace(runnerCount);
    fireMan.readyGo(new CountDownRaceExecution() {

        public void execute(CountDownRaceRunner resource) {
            final Object lockObj = resource.getLockObj();
            // for exception message
            String currentTable = null;
            Connection runnerConn = null;
            try {
                runnerConn = fittingDs.newConnection();
                prepareThreadDataSource(fittingDs, runnerConn);
                final DatabaseMetaData newMetaData = runnerConn.getMetaData();
                for (DfTableMeta tableMeta : tableList) {
                    final String tableKey = tableMeta.getTableFullQualifiedName();
                    synchronized (lockObj) {
                        if (_tableMetaDataSyncSet.contains(tableKey)) {
                            continue;
                        }
                        _tableMetaDataSyncSet.add(tableKey);
                    }
                    currentTable = tableKey;
                    doProcessTable(runnerConn, newMetaData, tableMeta);
                }
            } catch (SQLException e) {
                String msg = "Failed to get the table meta data: " + currentTable;
                throw new IllegalStateException(msg, e);
            } finally {
                if (runnerConn != null) {
                    try {
                        runnerConn.close();
                    } catch (SQLException e) {
                    }
                }
                DfDataSourceContext.clearDataSource();
            }
        }

        protected void prepareThreadDataSource(final DfFittingDataSource fittingDs, final Connection runnerConn) {
            if (DfDataSourceContext.isExistDataSource()) {
                return;
            }
            final Connection threadConn = new NotClosingConnectionWrapper(runnerConn);
            DfDataSourceContext.setDataSource(new HandlingDataSourceWrapper(fittingDs, new DataSourceHandler() {

                public Connection getConnection(DataSource dataSource) throws SQLException {
                    return threadConn;
                }
            }));
        }
    });
}
Also used : DataSourceHandler(org.dbflute.jdbc.DataSourceHandler) CountDownRaceRunner(org.dbflute.helper.thread.CountDownRaceRunner) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DfFittingDataSource(org.dbflute.helper.jdbc.connection.DfFittingDataSource) DataSource(javax.sql.DataSource) CountDownRace(org.dbflute.helper.thread.CountDownRace) CountDownRaceExecution(org.dbflute.helper.thread.CountDownRaceExecution) HandlingDataSourceWrapper(org.dbflute.jdbc.HandlingDataSourceWrapper) DfFittingDataSource(org.dbflute.helper.jdbc.connection.DfFittingDataSource) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta) NotClosingConnectionWrapper(org.dbflute.jdbc.NotClosingConnectionWrapper)

Aggregations

Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 DfFittingDataSource (org.dbflute.helper.jdbc.connection.DfFittingDataSource)1 CountDownRace (org.dbflute.helper.thread.CountDownRace)1 CountDownRaceExecution (org.dbflute.helper.thread.CountDownRaceExecution)1 CountDownRaceRunner (org.dbflute.helper.thread.CountDownRaceRunner)1 DataSourceHandler (org.dbflute.jdbc.DataSourceHandler)1 HandlingDataSourceWrapper (org.dbflute.jdbc.HandlingDataSourceWrapper)1 NotClosingConnectionWrapper (org.dbflute.jdbc.NotClosingConnectionWrapper)1 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)1