Search in sources :

Example 1 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project tomcat by apache.

the class TestJdbcInterceptorConfigParsing method testWhitespace.

@Test
public void testWhitespace() throws Exception {
    String interceptorConfig = "FirstInterceptor ; \n" + "SecondInterceptor (parm1  = value1 , parm2= value2 ) ; \n\n" + "\t org.cyb.ThirdInterceptor(parm1=value1);  \n" + "EmptyParmValInterceptor(parm1=  )";
    PoolProperties props = new PoolProperties();
    props.setJdbcInterceptors(interceptorConfig);
    InterceptorDefinition[] interceptorDefs = props.getJdbcInterceptorsAsArray();
    assertNotNull(interceptorDefs);
    // 5 items because parser automatically inserts TrapException interceptor to front of list
    assertEquals(interceptorDefs.length, 5);
    assertEquals(interceptorDefs[0].getClassName(), TrapException.class.getName());
    assertNotNull(interceptorDefs[1]);
    assertEquals(interceptorDefs[1].getClassName(), "FirstInterceptor");
    assertNotNull(interceptorDefs[2]);
    assertEquals(interceptorDefs[2].getClassName(), "SecondInterceptor");
    assertNotNull(interceptorDefs[3]);
    assertEquals(interceptorDefs[3].getClassName(), "org.cyb.ThirdInterceptor");
    Map<String, InterceptorProperty> secondProps = interceptorDefs[2].getProperties();
    assertNotNull(secondProps);
    assertEquals(secondProps.size(), 2);
    assertNotNull(secondProps.get("parm1"));
    assertEquals(secondProps.get("parm1").getValue(), "value1");
    assertNotNull(secondProps.get("parm2"));
    // Bug 54395
    assertEquals(secondProps.get("parm2").getValue(), "value2");
    Map<String, InterceptorProperty> thirdProps = interceptorDefs[3].getProperties();
    assertNotNull(thirdProps);
    assertEquals(thirdProps.size(), 1);
    assertNotNull(thirdProps.get("parm1"));
    assertEquals(thirdProps.get("parm1").getValue(), "value1");
    Map<String, InterceptorProperty> emptyParmValProps = interceptorDefs[4].getProperties();
    assertNotNull(emptyParmValProps);
    assertEquals(emptyParmValProps.size(), 1);
    assertNotNull(emptyParmValProps.get("parm1"));
    assertEquals(emptyParmValProps.get("parm1").getValue(), "");
}
Also used : InterceptorDefinition(org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition) InterceptorProperty(org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty) TrapException(org.apache.tomcat.jdbc.pool.TrapException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Test(org.junit.Test)

Example 2 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project tomcat by apache.

the class TestJdbcInterceptorConfigParsing method textExtraSemicolonBehavior.

@Test
public void textExtraSemicolonBehavior() {
    // This one DOES get an extra/empty definition
    PoolProperties props = new PoolProperties();
    props.setJdbcInterceptors(";EmptyLeadingSemiInterceptor");
    InterceptorDefinition[] jiDefs = props.getJdbcInterceptorsAsArray();
    assertNotNull(jiDefs);
    assertEquals(jiDefs.length, 3);
    // This one does NOT get an extra/empty definition (no trailing whitespace)
    props = new PoolProperties();
    props.setJdbcInterceptors("EmptyTrailingSemiInterceptor;");
    jiDefs = props.getJdbcInterceptorsAsArray();
    assertNotNull(jiDefs);
    assertEquals(jiDefs.length, 2);
    // This one DOES get an extra/empty definition (with trailing whitespace)
    props = new PoolProperties();
    props.setJdbcInterceptors("EmptyTrailingSemiInterceptor; ");
    jiDefs = props.getJdbcInterceptorsAsArray();
    assertNotNull(jiDefs);
    assertEquals(jiDefs.length, 3);
}
Also used : InterceptorDefinition(org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Test(org.junit.Test)

Example 3 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project tomcat by apache.

the class TestJdbcInterceptorConfigParsing method testBasic.

@Test
public void testBasic() throws Exception {
    String interceptorConfig = "FirstInterceptor;SecondInterceptor(parm1=value1,parm2=value2)";
    PoolProperties props = new PoolProperties();
    props.setJdbcInterceptors(interceptorConfig);
    InterceptorDefinition[] interceptorDefs = props.getJdbcInterceptorsAsArray();
    assertNotNull(interceptorDefs);
    // 3 items because parser automatically inserts TrapException interceptor to front of list
    assertEquals(interceptorDefs.length, 3);
    assertEquals(interceptorDefs[0].getClassName(), TrapException.class.getName());
    assertNotNull(interceptorDefs[1]);
    assertEquals(interceptorDefs[1].getClassName(), "FirstInterceptor");
    assertNotNull(interceptorDefs[2]);
    assertEquals(interceptorDefs[2].getClassName(), "SecondInterceptor");
    Map<String, InterceptorProperty> secondProps = interceptorDefs[2].getProperties();
    assertNotNull(secondProps);
    assertEquals(secondProps.size(), 2);
    assertNotNull(secondProps.get("parm1"));
    assertEquals(secondProps.get("parm1").getValue(), "value1");
    assertNotNull(secondProps.get("parm2"));
    assertEquals(secondProps.get("parm2").getValue(), "value2");
}
Also used : InterceptorDefinition(org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition) InterceptorProperty(org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty) TrapException(org.apache.tomcat.jdbc.pool.TrapException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Test(org.junit.Test)

Example 4 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project tomcat by apache.

the class TestJdbcInterceptorConfigParsing method testExceptionOrNot.

/*
     * Some of these should probably be handled more cleanly by the parser, but a few known
     * exception scenarios are presented here just to document current behavior.  In many cases
     * failure in parsing will just be propagated to a definition that will fail later
     * when instantiated.  Should we be failing faster (and with more detail)?
     */
@Test
public void testExceptionOrNot() throws Exception {
    PoolProperties props = null;
    String[] exceptionInducingConfigs = { "EmptyParmsInterceptor()", "WhitespaceParmsInterceptor(   )" };
    for (String badConfig : exceptionInducingConfigs) {
        props = new PoolProperties();
        props.setJdbcInterceptors(badConfig);
        try {
            props.getJdbcInterceptorsAsArray();
            fail("Expected exception.");
        } catch (Exception e) {
        // Expected
        }
    }
    String[] noExceptionButInvalidConfigs = { "MalformedParmsInterceptor(=   )", "MalformedParmsInterceptor(  =)", "MalformedParmsInterceptor(", "MalformedParmsInterceptor( ", "MalformedParmsInterceptor)", "MalformedParmsInterceptor) ", "MalformedParmsInterceptor )" };
    for (String badConfig : noExceptionButInvalidConfigs) {
        props = new PoolProperties();
        props.setJdbcInterceptors(badConfig);
        try {
            props.getJdbcInterceptorsAsArray();
        } catch (Exception e) {
            fail("Unexpected exception.");
        }
    }
}
Also used : TrapException(org.apache.tomcat.jdbc.pool.TrapException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Test(org.junit.Test)

Example 5 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties 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)

Aggregations

PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)22 Test (org.junit.Test)9 DataSource (org.apache.tomcat.jdbc.pool.DataSource)8 Connection (java.sql.Connection)5 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 Properties (java.util.Properties)4 DataSource (javax.sql.DataSource)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)3 InterceptorDefinition (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition)3 TrapException (org.apache.tomcat.jdbc.pool.TrapException)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 InterceptorProperty (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty)2 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)1 DatabasePoolConfig (com.ctrip.platform.dal.dao.configure.DatabasePoolConfig)1