1 22 23 24 package com.mchange.v2.c3p0.test; 25 26 import java.sql.*; 27 import javax.sql.*; 28 import com.mchange.v2.c3p0.*; 29 import com.mchange.v1.db.sql.*; 30 import javax.naming.Reference ; 31 import javax.naming.Referenceable ; 32 import com.mchange.v2.naming.ReferenceableUtils; 33 import com.mchange.v2.ser.SerializableUtils; 34 import com.mchange.v2.c3p0.DriverManagerDataSource; 35 import com.mchange.v2.c3p0.PoolBackedDataSource; 36 37 38 public final class TestRefSerStuff 39 { 40 static void create(DataSource ds) throws SQLException 41 { 42 Connection con = null; 43 Statement stmt = null; 44 try 45 { 46 con = ds.getConnection(); 47 stmt = con.createStatement(); 48 stmt.executeUpdate("CREATE TABLE TRSS_TABLE ( a_col VARCHAR(16) )"); 49 } 50 finally 51 { 52 StatementUtils.attemptClose( stmt ); 53 ConnectionUtils.attemptClose( con ); 54 } 55 } 56 57 static void drop(DataSource ds) throws SQLException 58 { 59 Connection con = null; 60 Statement stmt = null; 61 try 62 { 63 con = ds.getConnection(); 64 stmt = con.createStatement(); 65 stmt.executeUpdate("DROP TABLE TRSS_TABLE"); 66 } 67 finally 68 { 69 StatementUtils.attemptClose( stmt ); 70 ConnectionUtils.attemptClose( con ); 71 } 72 } 73 74 static void doSomething(DataSource ds) throws SQLException 75 { 76 Connection con = null; 77 Statement stmt = null; 78 try 79 { 80 con = ds.getConnection(); 81 stmt = con.createStatement(); 82 int i = stmt.executeUpdate("INSERT INTO TRSS_TABLE VALUES ('" + 83 System.currentTimeMillis() + "')"); 84 if (i != 1) 85 throw new SQLException("Insert failed somehow strange!"); 86 } 87 finally 88 { 89 StatementUtils.attemptClose( stmt ); 90 ConnectionUtils.attemptClose( con ); 91 } 92 } 93 94 104 105 static void doTest(DataSource checkMe) throws Exception 106 { 107 doSomething( checkMe ); 108 System.err.println("\tcreated: " + checkMe); 109 DataSource afterSer = (DataSource) SerializableUtils.testSerializeDeserialize( checkMe ); 110 doSomething( afterSer ); 111 System.err.println("\tafter ser: " + afterSer ); 112 Reference ref = ((Referenceable ) checkMe).getReference(); 113 DataSource afterRef = (DataSource) ReferenceableUtils.referenceToObject( ref, 116 null, 117 null, 118 null ); 119 doSomething( afterRef ); 121 System.err.println("\tafter ref: " + afterRef ); 122 } 123 124 public static void main( String [] argv ) 125 { 126 if (argv.length > 0) 127 { 128 System.err.println( TestRefSerStuff.class.getName() + 129 " now requires no args. Please set everything in standard c3p0 config files."); 130 return; 131 } 132 133 155 156 try 157 { 158 DriverManagerDataSource dmds = new DriverManagerDataSource(); 159 try { drop( dmds ); } 163 catch (Exception e) 164 { } 165 create( dmds ); 166 167 System.err.println("DriverManagerDataSource:"); 168 doTest( dmds ); 169 170 WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource(); 171 wcpds.setNestedDataSource( dmds ); 172 PoolBackedDataSource pbds = new PoolBackedDataSource(); 173 pbds.setConnectionPoolDataSource( wcpds ); 174 175 System.err.println("PoolBackedDataSource:"); 176 doTest( pbds ); 177 178 ComboPooledDataSource cpds = new ComboPooledDataSource(); 179 doTest( cpds ); 180 } 181 catch ( Exception e ) 182 { e.printStackTrace(); } 183 } 184 185 } 186 | Popular Tags |