Hi all,
I use DBCP Connection pooling with the
Tomcat server.Number of database connections are keep on increasing , i am just wondering the way i am using the pool is coorect.
How do exactly pooling will work?
Does it creates all the connections at once and keep it ,please shud somelight on that .
Here is the
java code where i access the connection from
public class SingletonDatabaseConnection {
private static SingletonDatabaseConnection singleinstance = null;
DataSource ds;
Context ctx;
/**
*Constructor for the SingletonDatabaseConnection object
*/
private SingletonDatabaseConnection() {
System.out.println("Singleton object created");
}
/**
* Description of the Method
*
* @return Description of the Return Value
*/
public static SingletonDatabaseConnection instance() {
if (null == singleinstance) {
singleinstance = new SingletonDatabaseConnection();
}
return singleinstance;
}
/**
* Gets the dataSource attribute of the SingletonDatabaseConnection object
*
* @return The dataSource value
* @exception Exception Description of the Exception
*/
public DataSource getDataSource() throws Exception {
try {
ctx = new InitialContext();
if (ctx == null) {
throw new Exception("Boom - No Context");
}
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/NBDCORE_DATABASE");
} catch (Exception e) {
throw new SQLException();
}
return ds;
}
And here is the configuration i am using in the server.xml
<context>
<Context path="/xmlgateway" docBase="xmlgateway"
debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/NBDCORE_DATABASE"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/NBDCORE_DATABASE">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mssql max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>50</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MSSQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>bl1b1t</value>
</parameter>
<!-- Class name for mssql
JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://192.168.1.9:1433;DatabaseName=NBD_CORE_LIVE</value>
</parameter>
</ResourceParams>
</context>
And this is how access a connection from java file
SingletonDatabaseConnection sdc = SingletonDatabaseConnection.instance();
DataSource ds = sdc.getDataSource();
try{
if (ds != null) {
conn = ds.getConnection();
}
}
catch(Exception e)
{
}
Thanks