1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 32 public class TestAbandonedBasicDataSource extends TestBasicDataSource { 33 public TestAbandonedBasicDataSource(String testName) { 34 super(testName); 35 } 36 37 public static Test suite() { 38 return new TestSuite(TestAbandonedBasicDataSource.class); 39 } 40 41 public void setUp() throws Exception { 42 super.setUp(); 43 44 ds.setLogAbandoned(true); 47 ds.setRemoveAbandoned(true); 48 ds.setRemoveAbandonedTimeout(10000); 49 } 50 51 public void tearDown() throws Exception { 52 super.tearDown(); 53 } 55 56 58 private void getConnection1() throws Exception { 59 System.err.println("BEGIN getConnection1()"); 60 Connection conn = ds.getConnection(); 61 System.err.println("conn: " + conn); 62 System.err.println("END getConnection1()"); 63 } 64 65 private void getConnection2() throws Exception { 66 System.err.println("BEGIN getConnection2()"); 67 Connection conn = ds.getConnection(); 68 System.err.println("conn: " + conn); 69 System.err.println("END getConnection2()"); 70 } 71 72 private void getConnection3() throws Exception { 73 System.err.println("BEGIN getConnection3()"); 74 Connection conn = ds.getConnection(); 75 System.err.println("conn: " + conn); 76 System.err.println("END getConnection3()"); 77 } 78 79 public void testAbandoned() throws Exception { 80 ds.setRemoveAbandonedTimeout(0); 82 ds.setMaxActive(1); 83 84 System.err.println("----------------------------------------"); 85 getConnection1(); 86 getConnection2(); 87 getConnection3(); 88 System.err.println("----------------------------------------"); 89 } 90 91 public void testAbandonedClose() throws Exception { 92 ds.setRemoveAbandonedTimeout(0); 94 ds.setMaxActive(1); 95 96 Connection conn1 = getConnection(); 97 assertNotNull(conn1); 98 assertEquals(1, ds.getNumActive()); 99 100 Connection conn2 = getConnection(); 101 assertNotNull(conn2); 102 assertEquals(1, ds.getNumActive()); 103 104 try { conn2.close(); } catch (SQLException ex) { } 105 assertEquals(0, ds.getNumActive()); 106 107 try { conn1.close(); } catch (SQLException ex) { } 108 assertEquals(0, ds.getNumActive()); 109 } 110 111 public void testAbandonedCloseWithExceptions() throws Exception { 112 ds.setRemoveAbandonedTimeout(0); 114 ds.setMaxActive(1); 115 ds.setAccessToUnderlyingConnectionAllowed(true); 116 117 Connection conn1 = getConnection(); 118 assertNotNull(conn1); 119 assertEquals(1, ds.getNumActive()); 120 121 Connection conn2 = getConnection(); 122 assertNotNull(conn2); 123 assertEquals(1, ds.getNumActive()); 124 125 TesterConnection tconn1 = (TesterConnection) ((DelegatingConnection)conn1).getInnermostDelegate(); 127 tconn1.setFailure(new IOException ("network error")); 128 TesterConnection tconn2 = (TesterConnection) ((DelegatingConnection)conn2).getInnermostDelegate(); 129 tconn2.setFailure(new IOException ("network error")); 130 131 try { conn2.close(); } catch (SQLException ex) { } 132 assertEquals(0, ds.getNumActive()); 133 134 try { conn1.close(); } catch (SQLException ex) { } 135 assertEquals(0, ds.getNumActive()); 136 } 137 } 138 | Popular Tags |