Skip to content

Commit

Permalink
set query timeouts and fetch size in the new sql template classes fro…
Browse files Browse the repository at this point in the history
…m symmetric
  • Loading branch information
chenson42 committed Dec 28, 2011
1 parent 347cf19 commit 4f360b1
Show file tree
Hide file tree
Showing 28 changed files with 293 additions and 244 deletions.
Expand Up @@ -16,16 +16,17 @@
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License. */


package org.jumpmind.symmetric.db;

* under the License.
*/

package org.jumpmind.symmetric.db;

import java.sql.Connection;
import java.sql.SQLException;

import org.jumpmind.db.IDatabasePlatform;
import org.jumpmind.db.JdbcDatabasePlatformFactory;
import org.jumpmind.db.platform.DatabasePlatformSettings;
import org.jumpmind.db.platform.db2.Db2Platform;
import org.jumpmind.db.platform.derby.DerbyPlatform;
import org.jumpmind.db.platform.firebird.FirebirdPlatform;
Expand All @@ -52,162 +53,173 @@
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;


/**
* Factory class that is responsible for creating the appropriate {@link IDbDialect} for
* the configured database.
* Factory class that is responsible for creating the appropriate
* {@link IDbDialect} for the configured database.
*/
public class DbDialectFactory implements FactoryBean<IDbDialect>, BeanFactoryAware {

private static final ILog log = LogFactory.getLog(DbDialectFactory.class);
public class DbDialectFactory implements FactoryBean<IDbDialect>, BeanFactoryAware {

private static final ILog log = LogFactory.getLog(DbDialectFactory.class);

private IParameterService parameterService;
private String db2zSeriesProductVersion;

private JdbcTemplate jdbcTemplate;

private BeanFactory beanFactory;

private String db2zSeriesProductVersion;

private JdbcTemplate jdbcTemplate;

private BeanFactory beanFactory;

private int queryTimeout;


private int fetchSize;

private boolean forceDelimitedIdentifierModeOn = false;

private boolean forceDelimitedIdentifierModeOff = false;

private long tableCacheTimeoutInMs;

public IDbDialect getObject() throws Exception {

waitForAvailableDatabase();

IDatabasePlatform pf = JdbcDatabasePlatformFactory.createNewPlatformInstance(jdbcTemplate.getDataSource(),
org.jumpmind.log.LogFactory.getLog("org.jumpmind." + parameterService.getString(ParameterConstants.ENGINE_NAME)));


private long tableCacheTimeoutInMs;

public IDbDialect getObject() throws Exception {

waitForAvailableDatabase();

IDatabasePlatform pf = JdbcDatabasePlatformFactory.createNewPlatformInstance(
jdbcTemplate.getDataSource(),
new DatabasePlatformSettings(fetchSize, queryTimeout),
org.jumpmind.log.LogFactory.getLog("org.jumpmind."
+ parameterService.getString(ParameterConstants.ENGINE_NAME)));

if (forceDelimitedIdentifierModeOn) {
pf.setDelimitedIdentifierModeOn(true);
}

if (forceDelimitedIdentifierModeOff) {
pf.setDelimitedIdentifierModeOn(false);
}
pf.setClearCacheModelTimeoutInMs(tableCacheTimeoutInMs);

AbstractDbDialect dialect = null;

if (pf instanceof MySqlPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("mysqlDialect");
} else if (pf instanceof OraclePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("oracleDialect");
} else if (pf instanceof MsSqlPlatform) {

pf.setClearCacheModelTimeoutInMs(tableCacheTimeoutInMs);

AbstractDbDialect dialect = null;

if (pf instanceof MySqlPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("mysqlDialect");
} else if (pf instanceof OraclePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("oracleDialect");
} else if (pf instanceof MsSqlPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("msSqlDialect");
} else if (pf instanceof GreenplumPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("greenplumDialect");
} else if (pf instanceof PostgreSqlPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("postgresqlDialect");
} else if (pf instanceof DerbyPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("derbyDialect");
} else if (pf instanceof H2Platform) {
dialect = (AbstractDbDialect) beanFactory.getBean("h2Dialect");
} else if (pf instanceof SqLitePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("sqliteDialect");
} else if (pf instanceof HsqlDbPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("hsqldbDialect");
} else if (pf instanceof HsqlDb2Platform) {
dialect = (AbstractDbDialect) beanFactory.getBean("hsqldb2Dialect");
} else if (pf instanceof InformixPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("informixDialect");
} else if (pf instanceof Db2Platform) {
String currentDbProductVersion = JdbcDatabasePlatformFactory.getDatabaseProductVersion(jdbcTemplate
.getDataSource());
if (currentDbProductVersion.equals(db2zSeriesProductVersion)) {
dialect = (AbstractDbDialect) beanFactory.getBean("db2zSeriesDialect");
dialect = (AbstractDbDialect) beanFactory.getBean("greenplumDialect");
} else if (pf instanceof PostgreSqlPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("postgresqlDialect");
} else if (pf instanceof DerbyPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("derbyDialect");
} else if (pf instanceof H2Platform) {
dialect = (AbstractDbDialect) beanFactory.getBean("h2Dialect");
} else if (pf instanceof SqLitePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("sqliteDialect");
} else if (pf instanceof HsqlDbPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("hsqldbDialect");
} else if (pf instanceof HsqlDb2Platform) {
dialect = (AbstractDbDialect) beanFactory.getBean("hsqldb2Dialect");
} else if (pf instanceof InformixPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("informixDialect");
} else if (pf instanceof Db2Platform) {
String currentDbProductVersion = JdbcDatabasePlatformFactory
.getDatabaseProductVersion(jdbcTemplate.getDataSource());
if (currentDbProductVersion.equals(db2zSeriesProductVersion)) {
dialect = (AbstractDbDialect) beanFactory.getBean("db2zSeriesDialect");
} else {
int dbMajorVersion = JdbcDatabasePlatformFactory.getDatabaseMajorVersion(jdbcTemplate.getDataSource());
int dbMinorVersion = JdbcDatabasePlatformFactory.getDatabaseMinorVersion(jdbcTemplate.getDataSource());
if (dbMajorVersion < 9 || (dbMajorVersion == 9 && dbMinorVersion < 5)) {
dialect = (AbstractDbDialect) beanFactory.getBean("db2Dialect");
} else {
dialect = (AbstractDbDialect) beanFactory.getBean("db2v9Dialect");
}
}
} else if (pf instanceof FirebirdPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("firebirdDialect");
int dbMajorVersion = JdbcDatabasePlatformFactory
.getDatabaseMajorVersion(jdbcTemplate.getDataSource());
int dbMinorVersion = JdbcDatabasePlatformFactory
.getDatabaseMinorVersion(jdbcTemplate.getDataSource());
if (dbMajorVersion < 9 || (dbMajorVersion == 9 && dbMinorVersion < 5)) {
dialect = (AbstractDbDialect) beanFactory.getBean("db2Dialect");
} else {
dialect = (AbstractDbDialect) beanFactory.getBean("db2v9Dialect");
}
}
} else if (pf instanceof FirebirdPlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("firebirdDialect");
} else if (pf instanceof SybasePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("sybaseDialect");
} else if (pf instanceof InterbasePlatform) {
dialect = (AbstractDbDialect) beanFactory.getBean("interbaseDialect");
} else {
throw new DbNotSupportedException();
}

dialect.init(pf, queryTimeout, jdbcTemplate);
return dialect;
}

private void waitForAvailableDatabase() {
boolean success = false;
while (!success) {
try {
jdbcTemplate.execute(new ConnectionCallback<Object>() {
public Object doInConnection(Connection con) throws SQLException,
DataAccessException {
return null;
}
});
success = true;
} catch (CannotGetJdbcConnectionException ex) {
log.error("DatabaseConnectionException", ex.getMessage());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}

public Class<IDbDialect> getObjectType() {
return IDbDialect.class;
}

public boolean isSingleton() {
return true;
}

public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

/**
* Sets the database product version for zOS db2 from the properties file
*/
public void setDb2zSeriesProductVersion(String version) {
this.db2zSeriesProductVersion = version;
}

public void setQueryTimeout(int queryTimeout) {
this.queryTimeout = queryTimeout;
} else {
throw new DbNotSupportedException();
}

dialect.init(pf, queryTimeout, jdbcTemplate);
return dialect;
}


private void waitForAvailableDatabase() {
boolean success = false;
while (!success) {
try {
jdbcTemplate.execute(new ConnectionCallback<Object>() {
public Object doInConnection(Connection con) throws SQLException,
DataAccessException {
return null;
}
});
success = true;
} catch (CannotGetJdbcConnectionException ex) {
log.error("DatabaseConnectionException", ex.getMessage());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}

public Class<IDbDialect> getObjectType() {
return IDbDialect.class;
}

public boolean isSingleton() {
return true;
}

public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

/**
* Sets the database product version for zOS db2 from the properties file
*/
public void setDb2zSeriesProductVersion(String version) {
this.db2zSeriesProductVersion = version;
}

public void setQueryTimeout(int queryTimeout) {
this.queryTimeout = queryTimeout;
}

public void setFetchSize(int fetchSize) {
this.fetchSize = fetchSize;
}

public void setForceDelimitedIdentifierModeOn(boolean forceDelimitedIdentifierModeOn) {
this.forceDelimitedIdentifierModeOn = forceDelimitedIdentifierModeOn;
}

public void setForceDelimitedIdentifierModeOff(boolean forceDelimitedIdentifierModeOff) {
this.forceDelimitedIdentifierModeOff = forceDelimitedIdentifierModeOff;
}

public void setParameterService(IParameterService parameterService) {
this.parameterService = parameterService;
}

public void setTableCacheTimeoutInMs(long tableCacheTimeoutInMs) {
this.tableCacheTimeoutInMs = tableCacheTimeoutInMs;
}

}
Expand Up @@ -74,6 +74,7 @@
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="db2zSeriesProductVersion" value="$[sym.db2.zseries.version]" />
<property name="queryTimeout" value="$[sym.db.sql.query.timeout.seconds]"/>
<property name="fetchSize" value="$[sym.db.jdbc.streaming.results.fetch.size]" />
<property name="forceDelimitedIdentifierModeOn" value="$[sym.db.force.delimited.identifier.mode.on]"/>
<property name="forceDelimitedIdentifierModeOff" value="$[sym.db.force.delimited.identifier.mode.off]"/>
<property name="tableCacheTimeoutInMs" value="$[sym.cache.table.time.ms]"/>
Expand Down
Expand Up @@ -31,6 +31,7 @@
import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.platform.DatabasePlatformSettings;
import org.jumpmind.db.platform.db2.Db2Platform;
import org.jumpmind.db.platform.derby.DerbyPlatform;
import org.jumpmind.db.platform.firebird.FirebirdPlatform;
Expand Down Expand Up @@ -110,9 +111,9 @@ public class JdbcDatabasePlatformFactory {
FirebirdPlatform.DATABASENAME);
}

public static synchronized IDatabasePlatform createNewPlatformInstance(DataSource dataSource)
public static synchronized IDatabasePlatform createNewPlatformInstance(DataSource dataSource, DatabasePlatformSettings settings)
throws DdlException {
return createNewPlatformInstance(dataSource, null);
return createNewPlatformInstance(dataSource, settings, null);
}

/*
Expand All @@ -125,7 +126,7 @@ public static synchronized IDatabasePlatform createNewPlatformInstance(DataSourc
* @return The platform or <code>null</code> if the database is not
* supported
*/
public static synchronized IDatabasePlatform createNewPlatformInstance(DataSource dataSource, Log log)
public static synchronized IDatabasePlatform createNewPlatformInstance(DataSource dataSource, DatabasePlatformSettings settings, Log log)
throws DdlException {

if (log == null) {
Expand All @@ -139,8 +140,8 @@ public static synchronized IDatabasePlatform createNewPlatformInstance(DataSourc
Class<? extends IDatabasePlatform> clazz = findPlatformClass(nameVersion);

try {
Constructor<? extends IDatabasePlatform> construtor = clazz.getConstructor(DataSource.class, Log.class);
return construtor.newInstance(dataSource, log);
Constructor<? extends IDatabasePlatform> construtor = clazz.getConstructor(DataSource.class, DatabasePlatformSettings.class, Log.class);
return construtor.newInstance(dataSource, settings, log);
} catch (Exception e) {
throw new DdlException("Could not create a platform of type " + nameVersion[0], e);
}
Expand Down
Expand Up @@ -15,14 +15,17 @@ abstract public class AbstractJdbcDatabasePlatform extends AbstractDatabasePlatf

protected ISqlTemplate sqlTemplate;

public AbstractJdbcDatabasePlatform(DataSource dataSource, Log log) {
protected DatabasePlatformSettings settings;

public AbstractJdbcDatabasePlatform(DataSource dataSource, DatabasePlatformSettings settings, Log log) {
super(log);
this.dataSource = dataSource;
createSqlTemplate(dataSource);
this.settings = settings;
createSqlTemplate();
}

protected void createSqlTemplate(DataSource dataSource) {
this.sqlTemplate = new JdbcSqlTemplate(dataSource);
protected void createSqlTemplate() {
this.sqlTemplate = new JdbcSqlTemplate(dataSource, settings, null);
}

@Override
Expand Down

0 comments on commit 4f360b1

Please sign in to comment.