简体   繁体   中英

Error while using connection pooling

I am new to java. i have a java class which accesses the database through jdbc. In each method of this class, i am opening and closing the connection, which seems to be causing the performance hit. I have tried using the jdbc connection pooling but not sure how to proceed with the same. I have created a Server.xml and Context.xml file as below.

Server.xml

<GlobalNamingResources>
  <Resource type="javax.sql.DataSource"
            name="jdbc/TestDB"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url=""
            username=""
            password=""
/>
 </GlobalNamingResources>

Context.xml

<Context>
  <ResourceLink type="javax.sql.DataSource"
                name="jdbc/LocalTestDB"
                global="jdbc/TestDB"
/>
 </Context>

and i have created a test java class as below.

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class TestDBConnections {

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    DataSource datasource = (DataSource) envContext.lookup("jdbc/LocalTestDB");
    Connection con = datasource.getConnection();

}    

i am getting the below error :

Default constructor cannot handle exception type NamingException thrown by implicit super constructor. Must define an explicit constructor.

Please help me with it.

I am copying my configurations and it is working for me. You can change your configuration to same and i think it will work...

context.xml

<ResourceLink
        global="jdbc/ManagerDB"
        name="jdbc/ManagerDB"
        type="javax.sql.DataSource"/>

Server.xml

<Resource name="jdbc/ManagerDB"
              auth="Container"
              type="com.mchange.v2.c3p0.ComboPooledDataSource"
              factory="org.apache.naming.factory.BeanFactory"
              user="MQG7qJMthHrAYL1eoLkJlg=="
              password="Y3XTGQyJCRs9xY1/ZPvYiQ=="
              driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
              jdbcUrl="jdbc:sqlserver://;serverName=172.16.2.45;port=1433;DatabaseName=mgmt"
              preferredTestQuery="SELECT 1"
              testConnectionOnCheckout="false"
              maxPoolSize="75"  
              initialPoolSize="30" 
              numHelperThreads="5"
              maxStatementsPerConnection="1875"
              testConnectionOnCheckin="false"
              idleConnectionTestPeriod="300"
              checkoutTimeout="10000" /> 

Now below code sample can be used to get datasource through jndi lookup

Context initContext = new InitialContext();
DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/ManagerDB");

Note: 1) I have global value and name value same in resource link
2) I have used C3p0 datasource you can use default datasource of tomcat.

In server.xml we define global datasources. However this datasources cannot be used by any context . until unless they have a resource link defined in their context.xml for that global datasource. Therefore you must have a resource link with its "global" attribute value equivalent to value of "name" attribute in server.xml.

Auth attribute :- Specify whether the web Application code signs on to the corresponding resource manager programmatically, or whether the Container will sign on to the resource manager on behalf of the application. The value of this attribute must be Application or Container. This attribute is required if the web application will use a element in the web application deployment descriptor, but is optional if the application uses a instead.

When you start your tomcat this datasources will be created and connection will be present in the pool. You just have to use given code snippet inside context for which you have defined resource link. And you will get the required datasource and database connection from it.

Please use Exception handler. use try catch or Throws because db connection code. Use like this

try
{
Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    DataSource datasource = (DataSource) envContext.lookup("jdbc/LocalTestDB");
    Connection con = datasource.getConnection();
}

catch(Exception ex)
{

}

Do like this, limit of connection maxActive in server.xml like wise i mention below code.

 <GlobalNamingResources>
 <Resource type="javax.sql.DataSource"
        name="jdbc/TestDB"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        driverClassName="com.mysql.jdbc.Driver"
        url=""
        username=""
         password=""
maxActive="100" maxIdle="30" maxWait="10000"
/>
</GlobalNamingResources>

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