KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > test > 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: Use "Contact Us Form" on website
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 package uk.org.primrose.pool.test;
23
24 import java.sql.*;
25 import java.io.*;
26 import java.util.*;
27 import javax.sql.DataSource JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import uk.org.primrose.pool.standalone.*;
33
34 public class TestPoolControllerStandalone {
35     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
36         new TestPoolControllerStandalone().testIt();
37     }
38
39     public void testIt() throws IOException {
40         // Load primrose & the pools required using the appropriate config file
41
//PoolControllerStandalone.load("C:/java/jakarta-tomcat-5.0.25/conf/poolConfig.properties");
42

43         // Load primrose from a java.util.Properties file
44
// For ease, we'll demonstrate loading via a file,
45
// but this could be via any method tp create you Properties object.
46
// Note that the first properties that you load MUST be the admin settings.
47
// After loading that you may load the pool instances/
48

49         // Load the admin settings :
50
/* EG :
51             #########################
52             #### Admin settings ####
53             #########################
54             adminUser=none
55             adminPassword=none
56             adminWebManagementPort=8090
57             adminEmailAddresses=sedj@primrose.org.uk
58             adminEmailNotifcations=false
59             adminEmailSMTPServer=sedj.demon.co.uk
60             adminEmailCrisisAddress=sedj@primrose.org.uk
61             adminEmailMaxWarningNumber=5
62             adminEmailNotificationPeriod=10000
63         */

64
65         Properties p = new Properties();
66         p.load(new FileInputStream("C:/java/admin.properties"));
67         PoolControllerStandalone.load(p);
68
69         // Also note that you will only be able to load ONE pool/SQL datasource
70
// using this method PER properties file - because the java.util.Properties object cannot
71
// support muliple references (obviously) for keys
72

73         // Now load the first pool
74
/* EG :
75             ##### webmap pool instance #####
76             poolName=webmap
77             base=5
78             overflow=15
79             log=C:/java/logs/pools.log
80             idleTime=30000
81             messageLogging=false
82             sizeLogging=true
83             driverClass=com.mysql.jdbc.Driver
84             driverURL=jdbc:mysql://localhost:3306/test
85             user=
86             password=
87             cycleConnections=-1
88             killActiveConnectionsOverAge=100
89             */

90
91         // Load the first pool
92
p = new Properties();
93         p.load(new FileInputStream("C:/java/poolConfig1.properties"));
94         PoolControllerStandalone.load(p);
95
96         // Load a second pool
97
p = new Properties();
98         p.load(new FileInputStream("C:/java/poolConfig2.properties"));
99         PoolControllerStandalone.load(p);
100
101         // Once loaded, you may use the pooled connections as you would with any
102
// other JNDI based pooling mechanism.
103
// Eg, here is a class that makes a few calls to the database
104
// via the primrose pooled connections :
105

106         for (int i = 0; i < 10; i++) {
107             System.out.println("Kicking off thread number : " +i);
108             new TestThread().start();
109         }
110     }
111
112
113     // A simple class to demonstrate the pooling ...
114
class TestThread extends Thread JavaDoc {
115         public void run() {
116             try {
117               // Obtain a JNDI javax.naming.Context object
118
Context JavaDoc ctx = new InitialContext JavaDoc();
119               // Retrieve a DataSource object using JNDI
120
// Here the pool name "webmap" is an example poolname,
121
// and should be replaced with your own pool name
122
DataSource JavaDoc ds = (DataSource JavaDoc)ctx.lookup("java:comp/env/webmap");
123               // Extract a connection from the datasource
124
Connection c = ds.getConnection();
125               // use the connection as normal (eg)
126
// Get a Statement
127
Statement s = c.createStatement();
128               // Execute a query and get a ResultSet
129
ResultSet rs = s.executeQuery("show tables");
130               int cols = rs.getMetaData().getColumnCount();
131
132               while (rs.next()) {
133                  // do something
134
String JavaDoc line = "";
135                  for (int i = 1; i <= cols; i++) {
136                      line += rs.getString(i) +" ";
137                  }
138
139                  System.out.println(line);
140               }
141               // Return the Connection to the pool
142
rs.close();
143               s.close();
144               c.close();
145             } catch (NamingException JavaDoc ne) {
146                 System.out.println("Problems looking up the pool's JNDI contexts ...");
147                 ne.printStackTrace();
148             } catch (SQLException sqle) {
149                 System.out.println("Problems with the db ...");
150                 sqle.printStackTrace();
151             }
152         }
153     }
154 }
Popular Tags