public class TomcatJdbcPoolDataSourceBuilder extends DataSourceBuilderBase {
private static final Logger log = LoggerFactory.getLogger(TomcatJdbcPoolDataSourceBuilder.class);
@Override
public DataSource buildDataSource(String key, JdbcConfiguration jdbcConfig) {
PoolProperties p = new PoolProperties();
if (jdbcConfig.driverClassName != null) {
p.setDriverClassName(jdbcConfig.driverClassName);
}
log.warn("Building data source for " + jdbcConfig.jdbcUrl);
p.setUrl(jdbcConfig.jdbcUrl);
p.setUsername(jdbcConfig.username);
p.setPassword(jdbcConfig.password);
String sqlDebug = null;
if (jdbcConfig.extraProperties != null) {
sqlDebug = jdbcConfig.extraProperties.get("sql.debug");
}
Properties jdbcProperties = buildDbProperties(jdbcConfig);
if (!jdbcProperties.isEmpty()) {
try {
p.setDbProperties(jdbcProperties);
} catch (Exception e) {
throw new IllegalStateException("Unable to set JDBC properties", e);
}
}
if (!Strings.isNullOrEmpty(sqlDebug)) {
throw new UnsupportedOperationException();
// pooledDataSource.setLogStatementsEnabled(Boolean.parseBoolean(sqlDebug));
}
p.setMinIdle(1);
p.setInitialSize(1);
List<String> interceptors = Lists.newArrayList();
interceptors.add("org.apache.tomcat.jdbc.pool.interceptor.StatementCache");
if (!interceptors.isEmpty()) {
p.setJdbcInterceptors(Joiner.on(';').join(interceptors));
}
// p.setJmxEnabled(true);
// p.setTestWhileIdle(false);
// p.setTestOnBorrow(true);