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(), "");
}
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);
}
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");
}
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.");
}
}
}
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
}
}
}
}
Aggregations