简体   繁体   中英

c3p0 connection pooling in jsp tomcat with netbeans

I m performing a connection pooling operation in jsp. I created a static function in a particular class called MCE_Server.java and included the following

 public static void makeConnectionPool()
 {
    try
    {
        cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/mce_db");
        cpds.setUser("root");
        cpds.setPassword("xxxxxx");
        cpds.setMaxPoolSize(100);
        cpds.setMinPoolSize(10);
        cpds.setAcquireIncrement(20);
    } 
    catch (PropertyVetoException ex) 
    {

        Logger.getLogger(MCE_Server.class.getName()).log(Level.SEVERE, null, ex);
    }

 }

Following static function is called from a jsp page

http://................/dbActivatePage.jsp

where i have included the function

      <%@page import="xxxxx.MCE_Server"%>
      <html>
      .
      .
      .
      <body>
      <%
              MCE_Server.makeConnectionPool();
      %>
      .
      .
      .
      </body>
      </html>

I m planning to get the required Connection as per the static function included in MCE_Server.java as follows:

       public static Connection getConnectionfromPool() throws SQLException
       {
             return cpds.getConnection();
       }

ie whenever i need to get a connection. I'll include MCE_Server.getConnectionfromPool() .

Now the Problem im having is im receiving an error

java.sql.SQLException: Connections could not be acquired from the underlying database!

Why I m getting this......??

On Further trial and error method.... i found out that the statements below the code cpds = new ComboPooledDataSource(); is getting executed.

So, what might be the problem here. Is my approach correct ?

this is not a good approach. stuff inside <% ... %> of a JSP gets executed every time a client hits your page. a Connection pool should be created only once per web application, not once per-user request!

my favorite way to set up a Connection pool in a web-app is to set up a ServletContextListener that creates the pool in contextInitialized(ServletContextEvent sce), and also binds it to a name in the application scope (ie it sets a ServletContext attribute). the pool should be close()ed in the contextDestroyed method of ServletContextListener.

if that seems like too much work, just change your makeConnectionPool() to a private method and call it only from inside a static initializer block. get rid of <% MCE_Server.makeConnectionPool(); %> entirely. then makeConnectionPool() will be called only once, when the MCE_Server class is loaded. but since you never destroy the pool, you'll find leaks of Threads and other resources if you unload and hot redeploy your application (ie you modify and reload the war file without quitting the Servlet container's JVM).

Well looking at the error ur showing.

java.sql.SQLException: Connections could not be acquired from the underlying database! .

It seems like your c3p0 configuration is right although i agree with Waldman's idea of using ServletContextListener. In your case, I strongly believe that the problem has to do something with mysql class path. Please Check whether u have properly included the mysql connector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM