KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestPoolControllerStandalone


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email : sedj@primrose.org.uk
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22 import java.sql.*;
23 import java.io.*;
24 import java.util.*;
25 import javax.sql.DataSource JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import uk.org.primrose.pool.standalone.*;
31
32 public class TestPoolControllerStandalone {
33     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
34         new TestPoolControllerStandalone().testIt();
35     }
36
37     public void testIt() throws IOException {
38         // Either load primrose via a static config file
39
// that contains all the pool data and admin settings :
40
testUsingStaticPoolConfigFile();
41
42         // OR
43
// load primrose using a series of java.util.Properties objects :
44
//testUsingDynamicPropertiesObject();
45

46
47         // Once loaded, you may use the pooled connections as you would with any
48
// other JNDI based pooling mechanism.
49
// Eg, here is a class that makes a few calls to the database
50
// via the primrose pooled connections :
51
for (int i = 0; i < 10; i++) {
52             System.out.println("Kicking off thread number : " +i);
53             new TestThread().start();
54         }
55
56         // wait for the threads to finish
57
try {
58             Thread.sleep(3000);
59         } catch (InterrupedException ie) {
60             ie.printStackTrace(System.err);
61         }
62
63         // shutdown the pools
64
PoolControllerStandalone.shutdown();
65     }
66
67     public void testUsingStaticPoolConfigFile() throws IOException {
68         // Load primrose & the pools required using the appropriate config file
69
PoolControllerStandalone.load("C:/java/jakarta-tomcat-5.0.25/conf/poolConfig.properties");
70     }
71
72     public void testUsingDynamicPropertiesObject() throws IOException {
73         // Load primrose from a java.util.Properties object
74
// For ease, we'll demonstrate creating a Properties object from a file,
75
// but this could be via any method to create you Properties object (eg from LDAP server settings).
76
// Note that the first properties that you load MUST be the admin settings.
77
// After loading that you may load the pool instances.
78

79         // Load the admin settings :
80
/* EG :
81             #########################
82             #### Admin settings ####
83             #########################
84             adminUser=none
85             adminPassword=none
86             adminWebManagementPort=8090
87             adminEmailAddresses=sedj@primrose.org.uk
88             adminEmailNotifcations=false
89             adminEmailSMTPServer=sedj.demon.co.uk
90             adminEmailCrisisAddress=sedj@primrose.org.uk
91             adminEmailMaxWarningNumber=5
92             adminEmailNotificationPeriod=10000
93         */

94
95         Properties p = new Properties();
96         p.load(new FileInputStream("C:/java/admin.properties"));
97         PoolControllerStandalone.load(p);
98
99         // Also note that you will only be able to load ONE pool/SQL datasource
100
// using this method PER properties file - because the java.util.Properties object cannot
101
// support muliple references (obviously) for keys
102

103         // Now load the first pool
104
/* EG :
105             ##### webmap pool instance #####
106             poolName=webmap
107             base=5
108             overflow=15
109             log=C:/java/logs/pools.log
110             idleTime=30000
111             messageLogging=false
112             sizeLogging=true
113             driverClass=com.mysql.jdbc.Driver
114             driverURL=jdbc:mysql://localhost:3306/test
115             user=
116             password=
117             cycleConnections=-1
118             killActiveConnectionsOverAge=100
119             */

120
121         // Load the first pool
122
p = new Properties();
123         p.load(new FileInputStream("C:/java/poolConfig1.properties"));
124         PoolControllerStandalone.load(p);
125
126         // Load a second pool
127
p = new Properties();
128         p.load(new FileInputStream("C:/java/poolConfig2.properties"));
129         PoolControllerStandalone.load(p);
130
131     }
132
133
134     // A simple class to demonstrate the pooling ...
135
class TestThread extends Thread JavaDoc {
136         public void run() {
137             try {
138               // Obtain a JNDI javax.naming.Context object
139
Context JavaDoc ctx = new InitialContext JavaDoc();
140               // Retrieve a DataSource object using JNDI
141
// Here the pool name "webmap" is an example poolname,
142
// and should be replaced with your own pool name
143
DataSource JavaDoc ds = (DataSource JavaDoc)ctx.lookup("java:comp/env/webmap");
144               // Extract a connection from the datasource
145
Connection c = ds.getConnection();
146               // use the connection as normal (eg)
147
// Get a Statement
148
Statement s = c.createStatement();
149               // Execute a query and get a ResultSet
150
ResultSet rs = s.executeQuery("show tables");
151               int cols = rs.getMetaData().getColumnCount();
152
153               while (rs.next()) {
154                  // do something
155
String JavaDoc line = "";
156                  for (int i = 1; i <= cols; i++) {
157                      line += rs.getString(i) +" ";
158                  }
159
160                  System.out.println(line);
161               }
162               // Return the Connection to the pool
163
rs.close();
164               s.close();
165               c.close();
166             } catch (NamingException JavaDoc ne) {
167                 System.out.println("Problems looking up the pool's JNDI contexts ...");
168                 ne.printStackTrace();
169             } catch (SQLException sqle) {
170                 System.out.println("Problems with the db ...");
171                 sqle.printStackTrace();
172             }
173         }
174     }
175 }
Popular Tags