use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.
the class SimplePOJOExample method main.
public static void main(String[] args) throws Exception {
PoolConfiguration p = new PoolProperties();
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.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
Connection con = null;
try {
con = datasource.getConnection();
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.PoolConfiguration in project tomcat by apache.
the class ConnectionState method reset.
@Override
public void reset(ConnectionPool parent, PooledConnection con) {
if (parent == null || con == null) {
//we are resetting, reset our defaults
autoCommit = null;
transactionIsolation = null;
readOnly = null;
catalog = null;
return;
}
PoolConfiguration poolProperties = parent.getPoolProperties();
if (poolProperties.getDefaultTransactionIsolation() != DataSourceFactory.UNKNOWN_TRANSACTIONISOLATION) {
try {
if (transactionIsolation == null || transactionIsolation.intValue() != poolProperties.getDefaultTransactionIsolation()) {
con.getConnection().setTransactionIsolation(poolProperties.getDefaultTransactionIsolation());
transactionIsolation = Integer.valueOf(poolProperties.getDefaultTransactionIsolation());
}
} catch (SQLException x) {
transactionIsolation = null;
log.error("Unable to reset transaction isolation state to connection.", x);
}
}
if (poolProperties.getDefaultReadOnly() != null) {
try {
if (readOnly == null || readOnly.booleanValue() != poolProperties.getDefaultReadOnly().booleanValue()) {
con.getConnection().setReadOnly(poolProperties.getDefaultReadOnly().booleanValue());
readOnly = poolProperties.getDefaultReadOnly();
}
} catch (SQLException x) {
readOnly = null;
log.error("Unable to reset readonly state to connection.", x);
}
}
if (poolProperties.getDefaultAutoCommit() != null) {
try {
if (autoCommit == null || autoCommit.booleanValue() != poolProperties.getDefaultAutoCommit().booleanValue()) {
con.getConnection().setAutoCommit(poolProperties.getDefaultAutoCommit().booleanValue());
autoCommit = poolProperties.getDefaultAutoCommit();
}
} catch (SQLException x) {
autoCommit = null;
log.error("Unable to reset autocommit state to connection.", x);
}
}
if (poolProperties.getDefaultCatalog() != null) {
try {
if (catalog == null || (!catalog.equals(poolProperties.getDefaultCatalog()))) {
con.getConnection().setCatalog(poolProperties.getDefaultCatalog());
catalog = poolProperties.getDefaultCatalog();
}
} catch (SQLException x) {
catalog = null;
log.error("Unable to reset default catalog state to connection.", x);
}
}
}
use of org.apache.tomcat.jdbc.pool.PoolConfiguration in project tomcat by apache.
the class Bug51582 method main.
public static void main(String[] args) throws SQLException {
org.apache.tomcat.jdbc.pool.DataSource datasource = null;
PoolConfiguration p = new DefaultProperties();
p.setJmxEnabled(true);
p.setTestOnBorrow(false);
p.setTestOnReturn(false);
p.setValidationInterval(1000);
p.setTimeBetweenEvictionRunsMillis(2000);
p.setMaxWait(2000);
p.setMinEvictableIdleTimeMillis(1000);
datasource = new org.apache.tomcat.jdbc.pool.DataSource();
datasource.setPoolProperties(p);
datasource.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=200)");
ConnectionPool pool = datasource.createPool();
Connection con = pool.getConnection();
Statement st = con.createStatement();
try {
st.execute("DROP ALIAS SLEEP");
} catch (Exception ignore) {
// Ignore
}
st.execute("CREATE ALIAS SLEEP AS $$\nboolean sleep() {\n try {\n Thread.sleep(10000);\n return true; } catch (Exception x) {\n return false;\n }\n}\n$$;");
st.close();
con.close();
int iter = 0;
while ((iter++) < 10) {
final Connection connection = pool.getConnection();
final CallableStatement s = connection.prepareCall("{CALL SLEEP()}");
List<Thread> threadList = new ArrayList<>();
for (int l = 0; l < 3; l++) {
final int i = l;
Thread thread = new Thread() {
@Override
public void run() {
try {
if (i == 0) {
Thread.sleep(1000);
s.cancel();
} else if (i == 1) {
// or use some other statement which will block
// for a longer time
long start = System.currentTimeMillis();
System.out.println("[" + getName() + "] Calling SP SLEEP");
s.execute();
System.out.println("[" + getName() + "] Executed SP SLEEP [" + (System.currentTimeMillis() - start) + "]");
} else {
Thread.sleep(1000);
connection.close();
}
} catch (InterruptedException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
};
threadList.add(thread);
thread.start();
}
for (Thread t : threadList) {
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
use of org.apache.tomcat.jdbc.pool.PoolConfiguration 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.PoolConfiguration in project tomcat by apache.
the class DefaultTestCase method createDefaultDataSource.
public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
org.apache.tomcat.jdbc.pool.DataSource datasource = null;
PoolConfiguration p = new DefaultProperties();
p.setFairQueue(false);
p.setJmxEnabled(false);
p.setTestWhileIdle(false);
p.setTestOnBorrow(false);
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(threadcount);
p.setInitialSize(threadcount);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(10);
p.setMinEvictableIdleTimeMillis(10000);
p.setMinIdle(threadcount);
p.setLogAbandoned(false);
p.setRemoveAbandoned(false);
datasource = new org.apache.tomcat.jdbc.pool.DataSource();
datasource.setPoolProperties(p);
return datasource;
}
Aggregations