1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 33 public class TestPStmtPoolingBasicDataSource extends TestBasicDataSource { 34 public TestPStmtPoolingBasicDataSource(String testName) { 35 super(testName); 36 } 37 38 public static Test suite() { 39 return new TestSuite(TestPStmtPoolingBasicDataSource.class); 40 } 41 42 public void setUp() throws Exception { 43 super.setUp(); 44 45 ds.setPoolPreparedStatements(true); 47 ds.setMaxOpenPreparedStatements(2); 48 } 49 50 public void tearDown() throws Exception { 51 super.tearDown(); 52 } 54 55 public void testPreparedStatementPooling() throws Exception { 56 Connection conn = getConnection(); 57 assertNotNull(conn); 58 59 PreparedStatement stmt1 = conn.prepareStatement("select 'a' from dual"); 60 assertNotNull(stmt1); 61 62 PreparedStatement stmt2 = conn.prepareStatement("select 'b' from dual"); 63 assertNotNull(stmt2); 64 65 assertTrue(stmt1 != stmt2); 66 67 PreparedStatement stmt3 = null; 69 try { 70 stmt3 = conn.prepareStatement("select 'c' from dual"); 71 fail("expected SQLException"); 72 } 73 catch (SQLException e) {} 74 75 stmt2.close(); 77 78 stmt3 = conn.prepareStatement("select 'c' from dual"); 80 assertNotNull(stmt3); 81 assertTrue(stmt3 != stmt1); 82 assertTrue(stmt3 != stmt2); 83 84 stmt1.close(); 86 PreparedStatement stmt4 = conn.prepareStatement("select 'a' from dual"); 87 assertNotNull(stmt4); 88 } 89 90 public void testPStmtCatalog() throws Exception { 93 Connection conn = getConnection(); 94 conn.setCatalog("catalog1"); 95 DelegatingPreparedStatement stmt1 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual"); 96 TesterPreparedStatement inner1 = (TesterPreparedStatement) stmt1.getInnermostDelegate(); 97 assertEquals("catalog1", inner1.getCatalog()); 98 stmt1.close(); 99 100 conn.setCatalog("catalog2"); 101 DelegatingPreparedStatement stmt2 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual"); 102 TesterPreparedStatement inner2 = (TesterPreparedStatement) stmt2.getInnermostDelegate(); 103 assertEquals("catalog2", inner2.getCatalog()); 104 stmt2.close(); 105 106 conn.setCatalog("catalog1"); 107 DelegatingPreparedStatement stmt3 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual"); 108 TesterPreparedStatement inner3 = (TesterPreparedStatement) stmt1.getInnermostDelegate(); 109 assertEquals("catalog1", inner3.getCatalog()); 110 stmt3.close(); 111 112 assertNotSame(inner1, inner2); 113 assertSame(inner1, inner3); 114 } 115 116 public void testPStmtPoolingWithNoClose() throws Exception { 117 ds.setMaxActive(1); ds.setMaxIdle(1); 119 ds.setAccessToUnderlyingConnectionAllowed(true); 120 Connection conn1 = getConnection(); 121 assertNotNull(conn1); 122 assertEquals(1, ds.getNumActive()); 123 assertEquals(0, ds.getNumIdle()); 124 125 PreparedStatement stmt1 = conn1.prepareStatement("select 'a' from dual"); 126 assertNotNull(stmt1); 127 128 Statement inner1 = ((DelegatingPreparedStatement) stmt1).getInnermostDelegate(); 129 assertNotNull(inner1); 130 131 stmt1.close(); 132 133 Connection conn2 = conn1; 134 assertNotNull(conn2); 135 assertEquals(1, ds.getNumActive()); 136 assertEquals(0, ds.getNumIdle()); 137 138 PreparedStatement stmt2 = conn2.prepareStatement("select 'a' from dual"); 139 assertNotNull(stmt2); 140 141 Statement inner2 = ((DelegatingPreparedStatement) stmt2).getInnermostDelegate(); 142 assertNotNull(inner2); 143 144 assertSame(inner1, inner2); 145 } 146 147 public void testPStmtPoolingAccrossClose() throws Exception { 148 ds.setMaxActive(1); ds.setMaxIdle(1); 150 ds.setAccessToUnderlyingConnectionAllowed(true); 151 Connection conn1 = getConnection(); 152 assertNotNull(conn1); 153 assertEquals(1, ds.getNumActive()); 154 assertEquals(0, ds.getNumIdle()); 155 156 PreparedStatement stmt1 = conn1.prepareStatement("select 'a' from dual"); 157 assertNotNull(stmt1); 158 159 Statement inner1 = ((DelegatingPreparedStatement) stmt1).getInnermostDelegate(); 160 assertNotNull(inner1); 161 162 stmt1.close(); 163 conn1.close(); 164 165 assertEquals(0, ds.getNumActive()); 166 assertEquals(1, ds.getNumIdle()); 167 168 Connection conn2 = getConnection(); 169 assertNotNull(conn2); 170 assertEquals(1, ds.getNumActive()); 171 assertEquals(0, ds.getNumIdle()); 172 173 PreparedStatement stmt2 = conn2.prepareStatement("select 'a' from dual"); 174 assertNotNull(stmt2); 175 176 Statement inner2 = ((DelegatingPreparedStatement) stmt2).getInnermostDelegate(); 177 assertNotNull(inner2); 178 179 assertSame(inner1, inner2); 180 } 181 } 182 | Popular Tags |