KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 public final class PSLoadPoolBackedDataSource
33 {
34     final static String JavaDoc INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )";
35     final static String JavaDoc SELECT_STMT = "SELECT count(*) FROM testpbds";
36     final static String JavaDoc DELETE_STMT = "DELETE FROM testpbds";
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( PSLoadPoolBackedDataSource.class.getName() +
46                                 " now requires no args. Please set everything in standard c3p0 config files.");
47             return;
48         }
49
50         String JavaDoc jdbc_url = null;
51         String JavaDoc username = null;
52         String JavaDoc password = null;
53
54         /*
55     if (argv.length == 3)
56         {
57         jdbc_url = argv[0];
58         username = argv[1];
59         password = argv[2];
60         }
61     else if (argv.length == 1)
62         {
63         jdbc_url = argv[0];
64         username = null;
65         password = null;
66         }
67     else
68         usage();
69     
70     if (! jdbc_url.startsWith("jdbc:") )
71         usage();
72        */

73     
74     try
75         {
76         //DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
77
//DataSource ds_unpooled = new FreezableDriverManagerDataSource();
78

79         DataSource ds_unpooled = DataSources.unpooledDataSource();
80         ds = DataSources.pooledDataSource( ds_unpooled );
81
82         //new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
83

84         Connection con = null;
85         Statement stmt = null;
86
87         try
88             {
89             con = ds_unpooled.getConnection();
90             stmt = con.createStatement();
91             stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
92             }
93         catch (SQLException e)
94             {
95             e.printStackTrace();
96             System.err.println("relation testpbds already exists, or something " +
97                        "bad happened.");
98             }
99         finally
100             {
101             StatementUtils.attemptClose( stmt );
102             ConnectionUtils.attemptClose( con );
103             }
104
105         //for (int i = 0; i < 5; ++i)
106
for (int i = 0; i < 50; ++i)
107             {
108             Thread JavaDoc t = new ChurnThread();
109             t.start();
110             System.out.println("THREAD MADE [" + i + "]");
111             Thread.sleep(1000);
112             }
113         
114         }
115     catch (Exception JavaDoc e)
116         { e.printStackTrace(); }
117     }
118
119     static class ChurnThread extends Thread JavaDoc
120     {
121     public void run()
122     {
123         try
124         {
125             while(true)
126             {
127                 Connection con = null;
128                 try
129                 {
130                     con = ds.getConnection();
131                     int select = random.nextInt(3);
132                     switch (select)
133                     {
134                     case 0:
135                         executeSelect( con );
136                         break;
137                     case 1:
138                         executeInsert( con );
139                         break;
140                     case 2:
141                         executeDelete( con );
142                         break;
143                     }
144                 }
145                 catch (Exception JavaDoc e)
146                 { e.printStackTrace(); }
147                 finally
148                 { ConnectionUtils.attemptClose( con ); }
149
150                 //Thread.sleep( random.nextInt( 1000 ) );
151
}
152         }
153         catch (Exception JavaDoc e)
154         { e.printStackTrace(); }
155     }
156     }
157
158     static void executeInsert(Connection con) throws SQLException
159     {
160     PreparedStatement pstmt = null;
161     try
162         {
163         pstmt = con.prepareStatement(INSERT_STMT);
164         pstmt.setInt(1, random.nextInt());
165         pstmt.setInt(2, random.nextInt());
166         pstmt.executeUpdate();
167         System.out.println("INSERTION");
168         }
169     finally
170         {
171         // make sure forgetting this doesn't starve
172
// statement cache, as long as the connection
173
// closes...
174

175         StatementUtils.attemptClose( pstmt );
176         }
177     }
178
179     static void executeSelect(Connection con) throws SQLException
180     {
181     long l = System.currentTimeMillis();
182     PreparedStatement pstmt = null;
183     ResultSet rs = null;
184     try
185         {
186         pstmt = con.prepareStatement(SELECT_STMT);
187         rs = pstmt.executeQuery();
188         rs.next(); //we assume one row, one col
189
System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" +
190                    (System.currentTimeMillis() - l) + " msecs]");
191         }
192     finally
193         {
194         ResultSetUtils.attemptClose( rs );
195         StatementUtils.attemptClose( pstmt );
196         }
197     }
198
199     static void executeDelete(Connection con) throws SQLException
200     {
201     PreparedStatement pstmt = null;
202     ResultSet rs = null;
203     try
204         {
205         pstmt = con.prepareStatement(DELETE_STMT);
206         int deleted = pstmt.executeUpdate();
207         System.out.println("DELETE [" + deleted + " rows]");
208         }
209     finally
210         {
211         ResultSetUtils.attemptClose( rs );
212         StatementUtils.attemptClose( pstmt );
213         }
214     }
215     
216     /*
217     private static void usage()
218     {
219     System.err.println("java " +
220                "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
221                PSLoadPoolBackedDataSource.class.getName() +
222                " <jdbc_url> [<username> <password>]" );
223     System.exit(-1);
224     }
225     */

226 }
227
Popular Tags