KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
27 import java.sql.*;
28 import javax.sql.*;
29 import com.mchange.v2.c3p0.*;
30 import com.mchange.v1.db.sql.*;
31 import com.mchange.v2.c3p0.DriverManagerDataSource;
32
33 public final class LoadPoolBackedDataSource
34 {
35     final static int NUM_THREADS = 50;
36     final static int ITERATIONS_PER_THREAD = 1000;
37
38     static Random random = new Random();
39     static DataSource ds;
40
41     public static void main(String JavaDoc[] argv)
42     {
43         if (argv.length > 0)
44         {
45             System.err.println( LoadPoolBackedDataSource.class.getName() +
46                                 " now requires no args. Please set everything in standard c3p0 config files.");
47             return;
48         }
49         String JavaDoc jdbc_url = null;
50         String JavaDoc username = null;
51         String JavaDoc password = null;
52
53     /*
54     if (argv.length == 3)
55         {
56         jdbc_url = argv[0];
57         username = argv[1];
58         password = argv[2];
59         }
60     else if (argv.length == 1)
61         {
62         jdbc_url = argv[0];
63         username = null;
64         password = null;
65         }
66     else
67         usage();
68     
69     if (! jdbc_url.startsWith("jdbc:") )
70         usage();
71 */

72     
73     try
74         {
75         //DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
76
DataSource ds_unpooled = DataSources.unpooledDataSource();
77         ds = DataSources.pooledDataSource( ds_unpooled );
78
79         Connection con = null;
80         Statement stmt = null;
81
82         try
83             {
84             con = ds.getConnection();
85             stmt = con.createStatement();
86             stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
87             System.err.println( "LoadPoolBackedDataSource -- TEST SCHEMA CREATED" );
88             }
89         catch (SQLException e)
90             {
91             e.printStackTrace();
92             System.err.println("relation testpbds already exists, or something " +
93                        "bad happened.");
94             }
95         finally
96             {
97             StatementUtils.attemptClose( stmt );
98             ConnectionUtils.attemptClose( con );
99             }
100
101         Thread JavaDoc[] threads = new Thread JavaDoc[NUM_THREADS];
102         for (int i = 0; i < NUM_THREADS; ++i)
103             {
104             Thread JavaDoc t = new ChurnThread();
105             threads[i] = t;
106             t.start();
107             System.out.println("THREAD MADE [" + i + "]");
108             Thread.sleep(1000);
109             }
110         for (int i = 0; i < NUM_THREADS; ++i)
111             threads[i].join();
112         
113         }
114     catch (Exception JavaDoc e)
115         { e.printStackTrace(); }
116     finally
117         {
118         Connection con = null;
119         Statement stmt = null;
120
121         try
122             {
123             con = ds.getConnection();
124             stmt = con.createStatement();
125             stmt.executeUpdate("DROP TABLE testpbds");
126             System.err.println( "LoadPoolBackedDataSource -- TEST SCHEMA DROPPED" );
127             }
128         catch (Exception JavaDoc e)
129             {
130             e.printStackTrace();
131             }
132         finally
133             {
134             StatementUtils.attemptClose( stmt );
135             ConnectionUtils.attemptClose( con );
136             }
137         }
138     }
139
140     static class ChurnThread extends Thread JavaDoc
141     {
142     public void run()
143     {
144         try
145         {
146             for( int i = 0; i < ITERATIONS_PER_THREAD; ++i )
147             {
148                 Connection con = null;
149                 try
150                 {
151                     con = ds.getConnection();
152                     int select = random.nextInt(3);
153                     switch (select)
154                     {
155                     case 0:
156                         executeSelect( con );
157                         break;
158                     case 1:
159                         executeInsert( con );
160                         break;
161                     case 2:
162                         executeDelete( con );
163                         break;
164                     }
165                     PooledDataSource pds = (PooledDataSource) ds;
166                     System.out.println( pds.getNumConnectionsDefaultUser() );
167                     System.out.println( pds.getNumIdleConnectionsDefaultUser() );
168                     System.out.println( pds.getNumBusyConnectionsDefaultUser() );
169                     System.out.println( pds.getNumConnectionsAllUsers() );
170                 }
171                 finally
172                 { ConnectionUtils.attemptClose( con ); }
173
174                 //Thread.sleep( random.nextInt( 1000 ) );
175
}
176         }
177         catch (Exception JavaDoc e)
178         { e.printStackTrace(); }
179     }
180     }
181
182     static void executeInsert(Connection con) throws SQLException
183     {
184     Statement stmt = null;
185     try
186         {
187         stmt = con.createStatement();
188         stmt.executeUpdate("INSERT INTO testpbds VALUES ('" +
189                    random.nextInt() + "', '" +
190                    random.nextInt() + "')");
191         System.out.println("INSERTION");
192         }
193     finally
194         {
195         StatementUtils.attemptClose( stmt );
196         }
197     }
198
199     static void executeDelete(Connection con) throws SQLException
200     {
201     Statement stmt = null;
202     try
203         {
204         stmt = con.createStatement();
205         stmt.executeUpdate("DELETE FROM testpbds;");
206         System.out.println("DELETION");
207         }
208     finally
209         {
210         StatementUtils.attemptClose( stmt );
211         }
212     }
213
214     static void executeSelect(Connection con) throws SQLException
215     {
216     long l = System.currentTimeMillis();
217     Statement stmt = null;
218     ResultSet rs = null;
219     try
220         {
221         stmt = con.createStatement();
222         rs = stmt.executeQuery("SELECT count(*) FROM testpbds");
223         rs.next(); //we assume one row, one col
224
System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" +
225                    (System.currentTimeMillis() - l) + " msecs]");
226         }
227     finally
228         {
229         ResultSetUtils.attemptClose( rs );
230         StatementUtils.attemptClose( stmt );
231         }
232     }
233
234     private static void usage()
235     {
236     System.err.println("java " +
237                "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
238                LoadPoolBackedDataSource.class.getName() +
239                " <jdbc_url> [<username> <password>]" );
240     System.exit(-1);
241     }
242
243
244 }
245
Popular Tags