KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > test > RawConnectionOpTest


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.test;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import com.mchange.v2.c3p0.ComboPooledDataSource;
29 import com.mchange.v2.c3p0.C3P0ProxyConnection;
30 import com.mchange.v2.c3p0.C3P0ProxyStatement;
31 import com.mchange.v2.c3p0.util.TestUtils;
32
33 public final class RawConnectionOpTest
34 {
35     public static void main(String JavaDoc[] argv)
36     {
37     try
38         {
39         String JavaDoc jdbc_url = null;
40         String JavaDoc username = null;
41         String JavaDoc password = null;
42         
43         if (argv.length == 3)
44             {
45             jdbc_url = argv[0];
46             username = argv[1];
47             password = argv[2];
48             }
49         else if (argv.length == 1)
50             {
51             jdbc_url = argv[0];
52             username = null;
53             password = null;
54             }
55         else
56             usage();
57         
58         if (! jdbc_url.startsWith("jdbc:") )
59             usage();
60                     
61         ComboPooledDataSource cpds = new ComboPooledDataSource();
62         cpds.setJdbcUrl( jdbc_url );
63         cpds.setUser( username );
64         cpds.setPassword( password );
65         cpds.setMaxPoolSize( 10 );
66 // cpds.setUsesTraditionalReflectiveProxies( true );
67

68         C3P0ProxyConnection conn = (C3P0ProxyConnection) cpds.getConnection();
69         Method JavaDoc toStringMethod = Object JavaDoc.class.getMethod("toString", new Class JavaDoc[]{});
70         Method JavaDoc identityHashCodeMethod = System JavaDoc.class.getMethod("identityHashCode", new Class JavaDoc[] {Object JavaDoc.class});
71         System.out.println("rawConnection.toString() -> " +
72                    conn.rawConnectionOperation(toStringMethod, C3P0ProxyConnection.RAW_CONNECTION, new Object JavaDoc[]{}));
73         Integer JavaDoc ihc = (Integer JavaDoc) conn.rawConnectionOperation(identityHashCodeMethod, null, new Object JavaDoc[]{C3P0ProxyConnection.RAW_CONNECTION});
74         System.out.println("System.identityHashCode( rawConnection ) -> " + Integer.toHexString( ihc.intValue() ));
75
76         C3P0ProxyStatement stmt = (C3P0ProxyStatement) conn.createStatement();
77         System.out.println("rawStatement.toString() -> " +
78                    stmt.rawStatementOperation(toStringMethod, C3P0ProxyStatement.RAW_STATEMENT, new Object JavaDoc[]{}));
79         Integer JavaDoc ihc2 = (Integer JavaDoc) stmt.rawStatementOperation(identityHashCodeMethod, null, new Object JavaDoc[]{C3P0ProxyStatement.RAW_STATEMENT});
80         System.out.println("System.identityHashCode( rawStatement ) -> " + Integer.toHexString( ihc2.intValue() ));
81
82         conn.close();
83
84         for (int i = 0; i < 10; ++i)
85             {
86             C3P0ProxyConnection check = null;
87             try
88                 {
89                 check = (C3P0ProxyConnection) cpds.getConnection();
90                 //System.err.println( TestUtils.samePhysicalConnection( conn, check ) );
91
System.err.println( TestUtils.physicalConnectionIdentityHashCode( check ) == ihc.intValue() );
92                 }
93             finally
94                 { /* if (check != null) check.close(); */ }
95             }
96         }
97     catch (Exception JavaDoc e)
98         { e.printStackTrace(); }
99     }
100
101     private static void usage()
102     {
103     System.err.println("java " + RawConnectionOpTest.class.getName() + " \\");
104     System.err.println("\t<jdbc_driver_class> \\");
105     System.err.println("\t<jdbc_url> [<username> <password>]");
106     System.exit(-1);
107     }
108 }
109
Popular Tags