在Conenction Pool C3P0 中访问 PPAS
为了结合C3P0和PPAS进行测试,写了如下代码:
连接池部分:Connections.java
import java.beans.PropertyVetoException; import java.sql.Connection; import com.mchange.v2.c3p0.ComboPooledDataSource; public class Connections { private static ComboPooledDataSource ds = new ComboPooledDataSource(); static { ds.setJdbcUrl("jdbc:edb://192.168.66.129:5444/edb"); ds.setUser("enterprisedb"); ds.setPassword("enterprisedb"); ds.setAcquireIncrement(15); ds.setInitialPoolSize(20); ds.setMinPoolSize(10); ds.setMaxPoolSize(500); ds.setAcquireRetryAttempts(30); ds.setMaxIdleTime(0); ds.setCheckoutTimeout(0); try { ds.setDriverClass("com.edb.Driver"); } catch (PropertyVetoException e) { } ds.setMaxStatements(0); } public static Connection getConnection() { Connection conn = null; try { conn = ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conn; } }
访问Connection: test02.java
import java.sql.*; import java.io.*; import java.lang.Thread; public class test02{ public static void main(String[] args) { try{ for(;;){ Connection con; con = Connections.getConnection(); System.out.println("Got connection now!"); Thread.sleep(240000); Statement stmt = con.createStatement() ; System.out.println("Got Statement now!"); Thread.sleep(240000); ResultSet rs = stmt.executeQuery("SELECT * FROM a5") ; System.out.println("Got ResultSet now!"); Thread.sleep(240000); int i=10; while(rs.next()){ i--; if (i<0) break; System.out.println("Got Record now!"); Thread.sleep(5000); String id = rs.getString("ID"); System.out.println("id is:"+id); } if(rs != null){ try{ rs.close() ; }catch(SQLException e){ e.printStackTrace() ; } } if(stmt != null){ try{ stmt.close() ; }catch(SQLException e){ e.printStackTrace() ; } } if(con != null){ try{ con.close() ; }catch(SQLException e){ e.printStackTrace() ; } } }///end of for loop }catch(Exception exp){ exp.printStackTrace(); } /////end of whole try catch pair }/////////////end of main }//////////////////////end of class
测试的结果如下:
测试1 获得ResultSet后,切断Connection并保持这种切断状态。
可以正常显示所有的记录。
测试2 获得Statement后,切断Connection、2分钟后恢复连接,
可以正常显示所有的记录。
测试3 获得Connection后,切断Connection、2分钟后恢复连接,
可以正常显示所有的记录。
测试4 获得Connection后、切断连接,知道获得Statement、一直保持切断状态。然后再恢复连接
会发生如下错误:
com.edb.util.PSQLException:
at com.edb.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:815)
at com.edb.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:541)
at com.edb.jdbc2.AbstractJdbc2Statement.executeWithFlags
(AbstractJdbc2Statement.java:444)
at com.edb.jdbc2.AbstractJdbc2Statement.executeQuery
(AbstractJdbc2Statement.java:275)
at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery
(NewProxyStatement.java:397)
at test02.main(test02.java:20)
Caused by: java.net.SocketException:
Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.edb.core.VisibleBufferedInputStream.readMore
(VisibleBufferedInputStream.java:146)
at com.edb.core.VisibleBufferedInputStream.ensureBytes
(VisibleBufferedInputStream.java:115)
at com.edb.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:74)
at com.edb.core.PGStream.ReceiveChar(PGStream.java:298)
at com.edb.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2402)
at com.edb.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:789)
... 5 more