/** * Library name : Primrose - A Java Database Connection Pool. * Published by Ben Keeping, http://primrose.org.uk . * Copyright (C) 2004 Ben Keeping, primrose.org.uk * Email: Use "Contact Us Form" on website * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import uk.org.primrose.vendor.standalone.*; import uk.org.primrose.pool.core.PoolLoader; import javax.sql.*; import java.sql.*; import javax.naming.*; import java.util.List; /** * * This class demonstrates how you may load primrose in a standalone * Java application. * * It first loads a web console, to provide an interface for viewing pool activity * and stopping/restarting pools on the fly. * * Then it loads some pools from a defined pool coniguration file. * * Note that in your application, you do not have to load the web console - * it is entirely optional. * */ public class TestPrimroseStandalone { public static void main(String args[]) throws Exception, Throwable { if (args.length != 4) { System.out.println("java uk.org.primrose.test.TestPrimroseStandalone "); System.out.println("EG:\njava uk.org.primrose.test.TestPrimroseStandalone /usr/primrose/conf/primrose.conf 8090 /var/log/primrose_console.log verbose,info,warn,error,crisis"); System.exit(0); } // Start the web console PrimroseWebConsoleLoader.load(Integer.parseInt(args[1]), args[2], args[3]); // Load the pools List loadedPoolNames = PrimroseLoader.load(args[0], true); // Get a pool name from the first loaded pool // to run our test with String poolName = loadedPoolNames.get(0); new TestPrimroseStandalone().runTest(poolName); //Thread.sleep(10000); //System.exit(0); } public void runTest(String poolName) throws Throwable { int i = 0; while (i++ < 1) { new RunThread(poolName).start(); } } /** * * Perform a test on the newly loaded pool * */ class RunThread extends Thread { String poolName = null; public RunThread(String poolName) { this.poolName = poolName; } public void run() { try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/" +poolName); long now = System.currentTimeMillis(); Connection c = ds.getConnection(); System.out.println("got connection " +c +" in " +(System.currentTimeMillis() - now) +"ms from pool " +poolName); Thread.sleep(1000); c.close(); } catch (Exception e) { e.printStackTrace(); } } } }