We help IT Professionals succeed at work.

It's Hang.. Connection pooling..

ultraman7719
ultraman7719 asked
on
474 Views
Last Modified: 2008-01-09
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
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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?

Author

Commented:
and is it that the main reason causing the system hang??
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
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....

Author

Commented:
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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Whats the pool settings ie. how many connections does it have?

Author

Commented:
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...

Author

Commented:
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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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

Author

Commented:
how can i see the "class"?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
System.out.println(source.getConnection().getClass());
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> source org.apache.tomcat.dbcp.dbcp.BasicDataSource@3dfcb

you can already see here that you are using dbcp data sources

Author

Commented:
the connection setting that you have mentioned.. where can i specified it?

in the server.xml?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.