001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.test;
024:
025: import java.util.*;
026: import java.sql.*;
027: import javax.sql.*;
028: import com.mchange.v2.c3p0.*;
029: import com.mchange.v1.db.sql.*;
030: import com.mchange.v2.c3p0.DriverManagerDataSource;
031:
032: public final class LoadPoolBackedDataSource {
033: final static int NUM_THREADS = 50;
034: final static int ITERATIONS_PER_THREAD = 1000;
035:
036: static Random random = new Random();
037: static DataSource ds;
038:
039: public static void main(String[] argv) {
040: if (argv.length > 0) {
041: System.err
042: .println(LoadPoolBackedDataSource.class.getName()
043: + " now requires no args. Please set everything in standard c3p0 config files.");
044: return;
045: }
046: String jdbc_url = null;
047: String username = null;
048: String password = null;
049:
050: /*
051: if (argv.length == 3)
052: {
053: jdbc_url = argv[0];
054: username = argv[1];
055: password = argv[2];
056: }
057: else if (argv.length == 1)
058: {
059: jdbc_url = argv[0];
060: username = null;
061: password = null;
062: }
063: else
064: usage();
065:
066: if (! jdbc_url.startsWith("jdbc:") )
067: usage();
068: */
069:
070: try {
071: //DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
072: DataSource ds_unpooled = DataSources.unpooledDataSource();
073: ds = DataSources.pooledDataSource(ds_unpooled);
074:
075: Connection con = null;
076: Statement stmt = null;
077:
078: try {
079: con = ds.getConnection();
080: stmt = con.createStatement();
081: stmt
082: .executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
083: System.err
084: .println("LoadPoolBackedDataSource -- TEST SCHEMA CREATED");
085: } catch (SQLException e) {
086: e.printStackTrace();
087: System.err
088: .println("relation testpbds already exists, or something "
089: + "bad happened.");
090: } finally {
091: StatementUtils.attemptClose(stmt);
092: ConnectionUtils.attemptClose(con);
093: }
094:
095: Thread[] threads = new Thread[NUM_THREADS];
096: for (int i = 0; i < NUM_THREADS; ++i) {
097: Thread t = new ChurnThread();
098: threads[i] = t;
099: t.start();
100: System.out.println("THREAD MADE [" + i + "]");
101: Thread.sleep(1000);
102: }
103: for (int i = 0; i < NUM_THREADS; ++i)
104: threads[i].join();
105:
106: } catch (Exception e) {
107: e.printStackTrace();
108: } finally {
109: Connection con = null;
110: Statement stmt = null;
111:
112: try {
113: con = ds.getConnection();
114: stmt = con.createStatement();
115: stmt.executeUpdate("DROP TABLE testpbds");
116: System.err
117: .println("LoadPoolBackedDataSource -- TEST SCHEMA DROPPED");
118: } catch (Exception e) {
119: e.printStackTrace();
120: } finally {
121: StatementUtils.attemptClose(stmt);
122: ConnectionUtils.attemptClose(con);
123: }
124: }
125: }
126:
127: static class ChurnThread extends Thread {
128: public void run() {
129: try {
130: for (int i = 0; i < ITERATIONS_PER_THREAD; ++i) {
131: Connection con = null;
132: try {
133: con = ds.getConnection();
134: int select = random.nextInt(3);
135: switch (select) {
136: case 0:
137: executeSelect(con);
138: break;
139: case 1:
140: executeInsert(con);
141: break;
142: case 2:
143: executeDelete(con);
144: break;
145: }
146: PooledDataSource pds = (PooledDataSource) ds;
147: System.out.println(pds
148: .getNumConnectionsDefaultUser());
149: System.out.println(pds
150: .getNumIdleConnectionsDefaultUser());
151: System.out.println(pds
152: .getNumBusyConnectionsDefaultUser());
153: System.out.println(pds
154: .getNumConnectionsAllUsers());
155: } finally {
156: ConnectionUtils.attemptClose(con);
157: }
158:
159: //Thread.sleep( random.nextInt( 1000 ) );
160: }
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164: }
165: }
166:
167: static void executeInsert(Connection con) throws SQLException {
168: Statement stmt = null;
169: try {
170: stmt = con.createStatement();
171: stmt.executeUpdate("INSERT INTO testpbds VALUES ('"
172: + random.nextInt() + "', '" + random.nextInt()
173: + "')");
174: System.out.println("INSERTION");
175: } finally {
176: StatementUtils.attemptClose(stmt);
177: }
178: }
179:
180: static void executeDelete(Connection con) throws SQLException {
181: Statement stmt = null;
182: try {
183: stmt = con.createStatement();
184: stmt.executeUpdate("DELETE FROM testpbds;");
185: System.out.println("DELETION");
186: } finally {
187: StatementUtils.attemptClose(stmt);
188: }
189: }
190:
191: static void executeSelect(Connection con) throws SQLException {
192: long l = System.currentTimeMillis();
193: Statement stmt = null;
194: ResultSet rs = null;
195: try {
196: stmt = con.createStatement();
197: rs = stmt.executeQuery("SELECT count(*) FROM testpbds");
198: rs.next(); //we assume one row, one col
199: System.out.println("SELECT [count=" + rs.getInt(1)
200: + ", time=" + (System.currentTimeMillis() - l)
201: + " msecs]");
202: } finally {
203: ResultSetUtils.attemptClose(rs);
204: StatementUtils.attemptClose(stmt);
205: }
206: }
207:
208: private static void usage() {
209: System.err.println("java "
210: + "-Djdbc.drivers=<comma_sep_list_of_drivers> "
211: + LoadPoolBackedDataSource.class.getName()
212: + " <jdbc_url> [<username> <password>]");
213: System.exit(-1);
214: }
215:
216: }
|