KickJava   Java API By Example, From Geeks To Geeks.

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


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 OneThreadRepeatedInsertOrQueryTest
33 {
34     final static String JavaDoc INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )";
35     final static String JavaDoc SELECT_STMT = "SELECT count(*) FROM testpbds";
36
37     static Random random = new Random();
38     static DataSource ds;
39
40     public static void main(String JavaDoc[] argv)
41     {
42     String JavaDoc jdbc_url = null;
43     String JavaDoc username = null;
44     String JavaDoc password = null;
45     if (argv.length == 3)
46         {
47         jdbc_url = argv[0];
48         username = argv[1];
49         password = argv[2];
50         }
51     else if (argv.length == 1)
52         {
53         jdbc_url = argv[0];
54         username = null;
55         password = null;
56         }
57     else
58         usage();
59     
60     if (! jdbc_url.startsWith("jdbc:") )
61         usage();
62     
63     
64     try
65         {
66         DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
67         ds = DataSources.pooledDataSource( ds_unpooled );
68
69         Connection con = null;
70         Statement stmt = null;
71
72         try
73             {
74             con = ds.getConnection();
75             stmt = con.createStatement();
76             stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
77             }
78         catch (SQLException e)
79             {
80             e.printStackTrace();
81             System.err.println("relation testpbds already exists, or something " +
82                        "bad happened.");
83             }
84         finally
85             {
86             StatementUtils.attemptClose( stmt );
87             ConnectionUtils.attemptClose( con );
88             }
89
90         while(true)
91             {
92             con = null;
93             try
94                 {
95                 con = ds.getConnection();
96                 boolean select = random.nextBoolean();
97                 if (select)
98                     executeSelect( con );
99                 else
100                     executeInsert( con );
101                 }
102             catch (Exception JavaDoc e)
103                 { e.printStackTrace(); }
104             finally
105                 { ConnectionUtils.attemptClose( con ); }
106             
107             //Thread.sleep( random.nextInt( 1000 ) );
108
}
109         }
110     catch (Exception JavaDoc e)
111         { e.printStackTrace(); }
112     }
113
114     static void executeInsert(Connection con) throws SQLException
115     {
116     PreparedStatement pstmt = null;
117     try
118         {
119         pstmt = con.prepareStatement(INSERT_STMT);
120         pstmt.setInt(1, random.nextInt());
121         pstmt.setInt(2, random.nextInt());
122         pstmt.executeUpdate();
123         System.out.println("INSERTION");
124         }
125     finally
126         {
127         // make sure forgetting this doesn't starve
128
// statement cache, as long as the connection
129
// closes...
130

131         StatementUtils.attemptClose( pstmt );
132         }
133     }
134
135     static void executeSelect(Connection con) throws SQLException
136     {
137     long l = System.currentTimeMillis();
138     PreparedStatement pstmt = null;
139     ResultSet rs = null;
140     try
141         {
142         pstmt = con.prepareStatement(SELECT_STMT);
143         rs = pstmt.executeQuery();
144         rs.next(); //we assume one row, one col
145
System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" +
146                    (System.currentTimeMillis() - l) + " msecs]");
147         }
148     finally
149         {
150         ResultSetUtils.attemptClose( rs );
151         StatementUtils.attemptClose( pstmt );
152         }
153     }
154
155     private static void usage()
156     {
157     System.err.println("java " +
158                "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
159                OneThreadRepeatedInsertOrQueryTest.class.getName() +
160                " <jdbc_url> [<username> <password>]" );
161     System.exit(-1);
162     }
163 }
164
Popular Tags