Search in sources :

Example 86 with Connection

use of java.sql.Connection 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
            }
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 87 with Connection

use of java.sql.Connection in project tomcat by apache.

the class StarvationTest method testConnectionStarvation.

//    @Test
//    public void testDBCPConnectionStarvation() throws Exception {
//        config();
//        this.transferProperties();
//        this.tDatasource.getConnection().close();
//        javax.sql.DataSource datasource = this.tDatasource;
//        Connection con1 = datasource.getConnection();
//        Connection con2 = null;
//        try {
//            con2 = datasource.getConnection();
//            try {
//                con2.setCatalog("mysql");//make sure connection is valid
//            }catch (SQLException x) {
//                assertFalse("2nd Connection is not valid:"+x.getMessage(),true);
//            }
//            assertTrue("Connection 1 should be closed.",con1.isClosed()); //first connection should be closed
//        }catch (Exception x) {
//            assertFalse("Connection got starved:"+x.getMessage(),true);
//        }finally {
//            if (con2!=null) con2.close();
//        }
//    }
@Test
public void testConnectionStarvation() throws Exception {
    config();
    Connection con1 = datasource.getConnection();
    Connection con2 = null;
    try {
        con2 = datasource.getConnection();
        try {
            //make sure connection is valid
            con2.setCatalog("mysql");
        } catch (SQLException x) {
            Assert.assertFalse("2nd Connection is not valid:" + x.getMessage(), true);
        }
        //first connection should be closed
        Assert.assertTrue("Connection 1 should be closed.", con1.isClosed());
    } catch (Exception x) {
        Assert.assertFalse("Connection got starved:" + x.getMessage(), true);
    } finally {
        if (con2 != null)
            con2.close();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 88 with Connection

use of java.sql.Connection in project tomcat by apache.

the class TestConcurrency method testBrutalNonFair.

@Test
public void testBrutalNonFair() throws Exception {
    ds.getPoolProperties().setRemoveAbandoned(false);
    ds.getPoolProperties().setRemoveAbandonedTimeout(1);
    ds.getPoolProperties().setMinEvictableIdleTimeMillis(100);
    ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(10);
    ds.getConnection().close();
    final int iter = 100000 * 10;
    final AtomicInteger loopcount = new AtomicInteger(0);
    final Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                while (loopcount.incrementAndGet() < iter) {
                    Connection con = ds.getConnection();
                    con.close();
                }
            } catch (Exception x) {
                //stops the test
                loopcount.set(iter);
                x.printStackTrace();
            }
        }
    };
    Thread[] threads = new Thread[20];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(run);
    }
    for (int i = 0; i < threads.length; i++) {
        threads[i].start();
    }
    try {
        while (loopcount.get() < iter) {
            Assert.assertTrue("Size comparison(less than 11):", ds.getPool().getSize() <= 10);
            ds.getPool().testAllIdle();
            ds.getPool().checkAbandoned();
            ds.getPool().checkIdle();
        }
    } catch (Exception x) {
        //stops the test
        loopcount.set(iter);
        x.printStackTrace();
    }
    for (int i = 0; i < threads.length; i++) {
        threads[i].join();
    }
    System.out.println("Connect count:" + Driver.connectCount.get());
    System.out.println("DisConnect count:" + Driver.disconnectCount.get());
    Assert.assertEquals("Size comparison:", 10, ds.getPool().getSize());
    Assert.assertEquals("Idle comparison:", 10, ds.getPool().getIdle());
    Assert.assertEquals("Used comparison:", 0, ds.getPool().getActive());
    Assert.assertEquals("Connect count", 10, Driver.connectCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(java.sql.Connection) Test(org.junit.Test)

Example 89 with Connection

use of java.sql.Connection in project tomcat by apache.

the class AbandonPercentageTest method testDefaultAbandon.

@Test
public void testDefaultAbandon() throws Exception {
    this.datasource.setMaxActive(100);
    this.datasource.setMaxIdle(100);
    this.datasource.setInitialSize(0);
    this.datasource.getPoolProperties().setAbandonWhenPercentageFull(0);
    this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
    this.datasource.getPoolProperties().setRemoveAbandoned(true);
    this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
    Connection con = datasource.getConnection();
    Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
    Thread.sleep(2000);
    Assert.assertEquals("Number of connections active/busy should be 0", 0, datasource.getPool().getActive());
    con.close();
}
Also used : Connection(java.sql.Connection) Test(org.junit.Test)

Example 90 with Connection

use of java.sql.Connection in project tomcat by apache.

the class AbandonPercentageTest method testMaxedOutAbandon.

@Test
public void testMaxedOutAbandon() throws Exception {
    int size = 100;
    this.datasource.setMaxActive(size);
    this.datasource.setMaxIdle(size);
    this.datasource.setInitialSize(0);
    this.datasource.getPoolProperties().setAbandonWhenPercentageFull(100);
    this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
    this.datasource.getPoolProperties().setRemoveAbandoned(true);
    this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
    Connection con = datasource.getConnection();
    Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
    Thread.sleep(2000);
    Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
    con.close();
}
Also used : Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)6326 PreparedStatement (java.sql.PreparedStatement)2793 ResultSet (java.sql.ResultSet)2657 Test (org.junit.Test)2455 SQLException (java.sql.SQLException)2267 Properties (java.util.Properties)1188 Statement (java.sql.Statement)1078 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)397 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 DataSource (javax.sql.DataSource)211 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)192 IOException (java.io.IOException)158 Reader (java.io.Reader)144 DatabaseMetaData (java.sql.DatabaseMetaData)144 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 HashMap (java.util.HashMap)123 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)114 Timestamp (java.sql.Timestamp)113