use of org.apache.tomcat.jdbc.pool.DataSource in project tomcat by apache.
the class SimplePOJOAsyncExample method main.
public static void main(String[] args) throws Exception {
PoolConfiguration p = new PoolProperties();
p.setFairQueue(true);
p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
Connection con = null;
try {
Future<Connection> future = datasource.getConnectionAsync();
while (!future.isDone()) {
System.out.println("Connection is not yet available. Do some background work");
try {
//simulate work
Thread.sleep(100);
} catch (InterruptedException x) {
Thread.interrupted();
}
}
//should return instantly
con = future.get();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". Host:" + rs.getString("Host") + " User:" + rs.getString("User") + " Password:" + rs.getString("Password"));
}
rs.close();
st.close();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception ignore) {
// Ignore
}
}
}
}
use of org.apache.tomcat.jdbc.pool.DataSource in project pinot by linkedin.
the class DaoProviderUtil method init.
public static void init(File localConfigFile) {
PersistenceConfig configuration = createConfiguration(localConfigFile);
dataSource = new DataSource();
dataSource.setInitialSize(10);
dataSource.setDefaultAutoCommit(false);
dataSource.setMaxActive(100);
dataSource.setUsername(configuration.getDatabaseConfiguration().getUser());
dataSource.setPassword(configuration.getDatabaseConfiguration().getPassword());
dataSource.setUrl(configuration.getDatabaseConfiguration().getUrl());
dataSource.setDriverClassName(configuration.getDatabaseConfiguration().getDriver());
dataSource.setValidationQuery("select 1");
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(true);
// when returning connection to pool
dataSource.setTestOnReturn(true);
dataSource.setRollbackOnReturn(true);
// Timeout before an abandoned(in use) connection can be removed.
dataSource.setRemoveAbandonedTimeout(600_000);
dataSource.setRemoveAbandoned(true);
init(dataSource);
}
use of org.apache.tomcat.jdbc.pool.DataSource in project Prism-Bukkit by prism.
the class Prism method initDbPool.
/**
*
* @return
*/
public DataSource initDbPool() {
DataSource pool = null;
final String dns = "jdbc:mysql://" + config.getString("prism.mysql.hostname") + ":" + config.getString("prism.mysql.port") + "/" + config.getString("prism.mysql.database") + "?useUnicode=true&characterEncoding=UTF-8";
pool = new DataSource();
pool.setDriverClassName("com.mysql.jdbc.Driver");
pool.setUrl(dns);
pool.setUsername(config.getString("prism.mysql.username"));
pool.setPassword(config.getString("prism.mysql.password"));
pool.setInitialSize(config.getInt("prism.database.pool-initial-size"));
pool.setMaxActive(config.getInt("prism.database.max-pool-connections"));
pool.setMaxIdle(config.getInt("prism.database.max-idle-connections"));
pool.setMaxWait(config.getInt("prism.database.max-wait"));
pool.setRemoveAbandoned(true);
pool.setRemoveAbandonedTimeout(60);
pool.setTestOnBorrow(true);
pool.setValidationQuery("/* ping */SELECT 1");
pool.setValidationInterval(30000);
return pool;
}
use of org.apache.tomcat.jdbc.pool.DataSource in project spring-boot by spring-projects.
the class DataSourceJsonSerializationTests method serializerFactory.
@Test
public void serializerFactory() throws Exception {
DataSource dataSource = new DataSource();
SerializerFactory factory = BeanSerializerFactory.instance.withSerializerModifier(new GenericSerializerModifier());
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(factory);
String value = mapper.writeValueAsString(dataSource);
assertThat(value.contains("\"url\":")).isTrue();
}
use of org.apache.tomcat.jdbc.pool.DataSource in project Gargoyle by callakrsos.
the class ConnectionManager method getDataSource.
public static DataSource getDataSource(String driver, String url, String id, String pass) throws Exception {
if (dataSource == null) {
/*2016-08-10
* id도 입력안하는 경우가 있음 sqlite. by kyj
* */
if (ValueUtil.isEmpty(driver, url)) {
throw new GargoyleConnectionFailException("Check 'Datatabse Settings' on the 'Configuration tab' ");
}
dataSource = new DataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(id);
dataSource.setPassword(pass);
dataSource.setDefaultAutoCommit(false);
dataSource.setInitialSize(2);
dataSource.setLoginTimeout(3);
}
return dataSource;
}
Aggregations