Link to home
Start Free TrialLog in
Avatar of ultraman7719
ultraman7719

asked on

It's Hang.. Connection pooling..

Bro Tim, i copyrighted your code.. hope you dont mind. But when i run it, until certain call [press refresh button], i will not able to see the data. Done some troubleshooting and see its hand in the DataSource part..

Anyone can spot what is happending? And also, when i down the tomcat, i have this message, seems it's still holding the connection...

********
Feb 21, 2006 12:16:34 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 2 instance(s) to be deallocated
Feb 21, 2006 12:16:35 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 2 instance(s) to be deallocated
Feb 21, 2006 12:16:36 PM org.apache.catalina.core.StandardWrapper unload
INFO: Waiting for 2 instance(s) to be deallocated
******

Mycode:
..
public static int counter =0;
..
..
counter++;
DataSourceLocator ds = new DataSourceLocator();
Connection conn = ds.getConnection("jdbc/TestDB");
..
while (rs.next()) {
  System.out.println(counter + " Model " + rs.getString("Model"));
   System.out.println(counter + " Remark " + rs.getString("Remark"));
}
..
..

DataSourceLocator.java
===============
..
..
      public DataSourceLocator() throws javax.naming.NamingException {
            System.out.println("Initialition");
            jndiContext = new InitialContext();
            dataSources = new HashMap();
      }

      public static DataSourceLocator getInstance()
                  throws javax.naming.NamingException {
            return locator != null ? locator : (locator = new DataSourceLocator());
      }

      public Connection getConnection(String name) throws java.sql.SQLException,
                  javax.naming.NamingException {
            System.out.println("Preparing connection");
            System.out.println("Sources Map Size" + dataSources.size());
            
            DataSource source = (DataSource) dataSources.get(name);
      ==>      
            if (source == null) {
                  source = locateDataSource(name);
                      } else {
                     System.out.println("Connection is " + source.getConnection().toString());
            }
               ===>
                                return source.getConnection();
      }

      synchronized private DataSource locateDataSource(String name)
                  throws javax.naming.NamingException {
            System.out.println("Looking for DataSource");
            DataSource source = (DataSource) dataSources.get(name);
            if (source == null) {
                  source = (DataSource) jndiContext.lookup("java:comp/env/" + name);
                  Map newSources = new HashMap(dataSources);
                  newSources.put(name, source);
                  dataSources = newSources;
            } else {
                  System.out.println("DataSource HashCode " + source.hashCode());
            }
            System.out.println("source " + source.toString());
            return source;
      }

..
..

this is the output...it's hang in "==>" shown above..

Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
1 Model Iswara
1 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
2 Model Iswara
2 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
3 Model Iswara
3 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
4 Model Iswara
4 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
5 Model Iswara
5 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
6 Model Iswara
6 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
7 Model Iswara
7 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
Preparing rs.net()
8 Model Iswara
8 Remark BUKIMAK
Initialition
Preparing connection
Sources Map Size0
Looking for DataSource
source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ultraman7719
ultraman7719

ASKER

do i need to? i tot the Connection Pooling will maintain that and close for me automatically..

if i include it (close connection) in, then the other coming in to get the connection will not able to get it right?
and is it that the main reason causing the system hang??
you need to close them to tell the pool you are no longer using them.
Otherwise they cannot be reused, defeating the need for a pool in the 1st place
object, you are right.. after i have included the close() command in, it's working fine. HAHAH..

by the way, how am i make sure tat it's realy using the connection pool??

i have tri to observed the total connection to Mysql in the MySql Network connection, it's seems the counter is keep on increasing each time i load the page... how can i make sure or observed so that i can prove to the team of my people....
and also.. if i didnt close the connection, how come it's only can stand up to 8 connection and it'll hang there? the MySQL setting i put is able to hold up to 20 connection...

Any other place i can set?
Whats the pool settings ie. how many connections does it have?
i didnt include anything connection limit in the server.xml file. ( Is there where i should set the Max Connection)??

        <Context
            crossContext="true"
            docBase="C:/Documents and Settings/Foong/workspace/ConnectionPoolTest"
            path="/ConnectionPoolTest"
            reloadable="true"
            workDir="C:\Documents and Settings\Foong\workspace\ConnectionPoolTest\work">
            debug="5"  
        <Resource
                      auth="Container"
                      name="jdbc/TestDB"
                      type="javax.sql.DataSource"
                      driverClassName="com.mysql.jdbc.Driver"
                      password="password"
                      username="root"
            url="jdbc:mysql://localhost:3306/foongtest"/>
        </Context>


>>by the way, how am i make sure tat it's realy using the connection pool??
Object, i have tried to observed in the MySQL admin console. i can see the different alreday...

Object, one things i curious.. i tot i have to use "Singleton" class to do the connection pooling, but seemsi am not using any Singleton class also yet i can share the pool...

so, which approach is a beter approach?
> Object, i have tried to observed in the MySQL admin console. i can see the different alreday...

the class of the connection should also be differnt I'd say
how can i see the "class"?
System.out.println(source.getConnection().getClass());
> source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb

you can already see here that you are using dbcp data sources
the connection setting that you have mentioned.. where can i specified it?

in the server.xml?