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:
031: public final class PSLoadPoolBackedDataSource {
032: final static String INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )";
033: final static String SELECT_STMT = "SELECT count(*) FROM testpbds";
034: final static String DELETE_STMT = "DELETE FROM testpbds";
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(PSLoadPoolBackedDataSource.class.getName()
043: + " now requires no args. Please set everything in standard c3p0 config files.");
044: return;
045: }
046:
047: String jdbc_url = null;
048: String username = null;
049: String password = null;
050:
051: /*
052: if (argv.length == 3)
053: {
054: jdbc_url = argv[0];
055: username = argv[1];
056: password = argv[2];
057: }
058: else if (argv.length == 1)
059: {
060: jdbc_url = argv[0];
061: username = null;
062: password = null;
063: }
064: else
065: usage();
066:
067: if (! jdbc_url.startsWith("jdbc:") )
068: usage();
069: */
070:
071: try {
072: //DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
073: //DataSource ds_unpooled = new FreezableDriverManagerDataSource();
074:
075: DataSource ds_unpooled = DataSources.unpooledDataSource();
076: ds = DataSources.pooledDataSource(ds_unpooled);
077:
078: //new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
079:
080: Connection con = null;
081: Statement stmt = null;
082:
083: try {
084: con = ds_unpooled.getConnection();
085: stmt = con.createStatement();
086: stmt
087: .executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
088: } catch (SQLException e) {
089: e.printStackTrace();
090: System.err
091: .println("relation testpbds already exists, or something "
092: + "bad happened.");
093: } finally {
094: StatementUtils.attemptClose(stmt);
095: ConnectionUtils.attemptClose(con);
096: }
097:
098: //for (int i = 0; i < 5; ++i)
099: for (int i = 0; i < 50; ++i) {
100: Thread t = new ChurnThread();
101: t.start();
102: System.out.println("THREAD MADE [" + i + "]");
103: Thread.sleep(1000);
104: }
105:
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: static class ChurnThread extends Thread {
112: public void run() {
113: try {
114: while (true) {
115: Connection con = null;
116: try {
117: con = ds.getConnection();
118: int select = random.nextInt(3);
119: switch (select) {
120: case 0:
121: executeSelect(con);
122: break;
123: case 1:
124: executeInsert(con);
125: break;
126: case 2:
127: executeDelete(con);
128: break;
129: }
130: } catch (Exception e) {
131: e.printStackTrace();
132: } finally {
133: ConnectionUtils.attemptClose(con);
134: }
135:
136: //Thread.sleep( random.nextInt( 1000 ) );
137: }
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: }
142: }
143:
144: static void executeInsert(Connection con) throws SQLException {
145: PreparedStatement pstmt = null;
146: try {
147: pstmt = con.prepareStatement(INSERT_STMT);
148: pstmt.setInt(1, random.nextInt());
149: pstmt.setInt(2, random.nextInt());
150: pstmt.executeUpdate();
151: System.out.println("INSERTION");
152: } finally {
153: // make sure forgetting this doesn't starve
154: // statement cache, as long as the connection
155: // closes...
156:
157: StatementUtils.attemptClose(pstmt);
158: }
159: }
160:
161: static void executeSelect(Connection con) throws SQLException {
162: long l = System.currentTimeMillis();
163: PreparedStatement pstmt = null;
164: ResultSet rs = null;
165: try {
166: pstmt = con.prepareStatement(SELECT_STMT);
167: rs = pstmt.executeQuery();
168: rs.next(); //we assume one row, one col
169: System.out.println("SELECT [count=" + rs.getInt(1)
170: + ", time=" + (System.currentTimeMillis() - l)
171: + " msecs]");
172: } finally {
173: ResultSetUtils.attemptClose(rs);
174: StatementUtils.attemptClose(pstmt);
175: }
176: }
177:
178: static void executeDelete(Connection con) throws SQLException {
179: PreparedStatement pstmt = null;
180: ResultSet rs = null;
181: try {
182: pstmt = con.prepareStatement(DELETE_STMT);
183: int deleted = pstmt.executeUpdate();
184: System.out.println("DELETE [" + deleted + " rows]");
185: } finally {
186: ResultSetUtils.attemptClose(rs);
187: StatementUtils.attemptClose(pstmt);
188: }
189: }
190:
191: /*
192: private static void usage()
193: {
194: System.err.println("java " +
195: "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
196: PSLoadPoolBackedDataSource.class.getName() +
197: " <jdbc_url> [<username> <password>]" );
198: System.exit(-1);
199: }
200: */
201: }
|