Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2004

Tip: Looking for answers? Try searching our database.

DBPool.jar  /  Mysql  Creates a new pool for each connection

Thread view: 
Fred - 10 Jun 2004 17:33 GMT
Hi all,

I have looked everywhere for an answer....
Any help would ... help

followed the instructions from
http://www.thep.lu.se/~nicklas/base2/download/mysql_example4.zip

instaled the DBPool.jar ect
I lauched the ConnectionPool.java program all seemed good.

Now to simulate 2 users I ran it from 2 different dos prompt
looping in 100's of queries.

Somehow it seems to always create 2 different pools
instead of 2 connections (Initialized pool BaseDB twice)
see the logs and code below.
Did I forget something? Could it be a class loader problem
as the pool is I believe a singleton.

Thanks for any help

Fred,
London

Thu Jun 10 13:18:26 BST 2004: Already using JDBC driver com.mysql.jdbc.Driver
Thu Jun 10 13:18:26 BST 2004: Initialized pool BaseDB (pool=10,max=10,expiry=none)
Thu Jun 10 13:18:26 BST 2004: BaseDB: Created a new connection
Thu Jun 10 13:18:28 BST 2004: Already using JDBC driver com.mysql.jdbc.Driver
Thu Jun 10 13:18:28 BST 2004: Initialized pool BaseDB (pool=10,max=10,expiry=none)
Thu Jun 10 13:18:28 BST 2004: BaseDB: Created a new connection
Thu Jun 10 13:19:32 BST 2004: BaseDB: Destroyed connection
Thu Jun 10 13:19:41 BST 2004: BaseDB: Destroyed connection

code used

import java.io.*;
import java.util.*;
import snaq.db.*;

public class TimeDBPool
{
    public static void main( String args[] )
  {
        if (args.length == 0)
        {
            System.out.println("Please run with: java TimeDBPool 1 | 2 | 3 | 4");
            return;
        }
        try
        {

            // Open a properties file where connection info is stored
            FileInputStream infile = new FileInputStream( "dbpool.properties" );
            Properties props = new Properties();
            props.load(infile);

            // Connection info for MySQL
            String url = props.getProperty("BaseDB.url", "jdbc:mysql://localhost/base");
            String user = props.getProperty("BaseDB.user", "baseuser");
            String password = props.getProperty("BaseDB.password", "");
            String cs = url+"?user="+user+"&password="+password;

            // The SQL statement to execute
            //String sql = "SELECT * FROM customers ORDER BY Account_nb";
            String sql = "select * from customers";
           
           

            // Define useful objects and variables
            java.sql.Connection conn;
            java.sql.Statement stat;
            java.sql.ResultSet rs;
            long s1;
            long s2;
            int i;
            int ntries = 100;

            // Register MySQL driver
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            // Open the connection manager
            ConnectionPoolManager conMgr = ConnectionPoolManager.getInstance();
           
           

            // Test 1: DriverManager with open() and close()
            if (args[0].equals("1"))
            {
                s1 = new Date().getTime();
                for(i = 0; i<= ntries; i++)
                {
                    conn = java.sql.DriverManager.getConnection(cs);
                    conn.close();
                }
                s2 = new Date().getTime();
                System.out.println("Test 1: "+(s2 - s1)+" milliseconds");
            }

            // Test 2: DriverManager with open(), select() and close()
            if (args[0].equals("2"))
            {
                s1 = new Date().getTime();
                for(i = 0; i<= ntries; i++)
                {
                    conn = java.sql.DriverManager.getConnection(cs);
                    stat = conn.createStatement();
                    rs = stat.executeQuery(sql);
                    rs.close();
                    stat.close();
                    conn.close();
                }
                s2 = new Date().getTime();
                System.out.println("Test 2: "+(s2 - s1)+" milliseconds");
            }

            // Test 3: DBPool with open() and close()
            if (args[0].equals("3"))
            {
                s1 = new Date().getTime();
                for(i = 0; i<= ntries; i++)
                {
                    conn = conMgr.getConnection("BaseDB");
                    conn.close();
                }
                s2 = new Date().getTime();
                System.out.println("Test 3: "+(s2 - s1)+" milliseconds");
            }

            // Test 4: DBPool with open(), select() and close()
            if (args[0].equals("4"))
            {
                s1 = new Date().getTime();
                for(i = 0; i<= ntries; i++)
                {
                    conn = conMgr.getConnection("BaseDB");
                    stat = conn.createStatement();
                    rs = stat.executeQuery(sql);
                    rs.close();
                    stat.close();
                    conn.close();
                }
                s2 = new Date().getTime();
                System.out.println("Test 4: "+(s2 - s1)+" milliseconds");
            }

            conMgr.release();
        }
        catch (Exception ex)
        {
            System.out.println( "An exception has been intercepted" );
            ex.printStackTrace();
        }
    }
}
G Winstanley - 10 Jun 2004 22:52 GMT
Hi Fred,

See the email I sent to you. Basically it's down to two seperate instances
of the JVM being launched when java.exe is launched under two DOS prompts.

Stan

Signature

DBPool - JDBC Connection Pooling
http://www.snaq.net/java/DBPool

> Hi all,
>
[quoted text clipped - 151 lines]
>     }
> }
Fred - 21 Jun 2004 08:44 GMT
I will post my own response ...
Stan from snaq Sent me a mail, here it is.

Hi Fred,

There's a very simple answer to this one. Launching Java from two separate
Command Prompt windows launches two instances of the JVM which inevitably
leads to seperate ClassLoader instances. An example of a shared ClassLoader
would be servlets running on a web server. This situation is possible with
desktop applications too, but more tricky to implement as you need to create
an application launcher with Java code so that they can share the same
ClassLoader.

As an example of the problem try the following code:

public class StaticTest implements Runnable
{
private Thread runner;
private static int num = 0;

public StaticTest()
{
(runner = new Thread(this)).start();
}

public static void main(String[] args)
{
new StaticTest();
}

public void run()
{
while (num < 50)
{
System.out.println(++num);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

When you launch this in two seperate Command Prompt windows you will find
that they independently increment from 1 upwards. The variable "num" is
static which means it should be shared between classes of the same Class
type under the same VM instance, inferring that two VMs are in operation.

Stan

Signature

DBPool - JDBC Connection Pooling
http://www.snaq.net/java/DBPool



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.