Use the database connection pool provided by the container--jndi usage

Source: Internet
Author: User
Tags in domain tomcat server

Now that the connection to the database is not used very rarely, each project group may have its own database connection pool component, and each container provider also provides its own database connection pool, which is described below for Tomcat and WebLogic using Jndi database connection management.

The database information used in this article is as follows:

Database: MS SQL Server
Host:demo
Database:test
DriverName:com.microsoft.jdbc.sqlserver.SQLServerDriver
(Note: Database driven, WebLogic placed under $bea$/weblogic81/common/lib, Tomcat placed under its own project/webroot/web-inf/lib)
User:sa
Password:sa

Data Source: Jndi_test

1, server configuration:

The container provider now provides management tools that enable you to configure database connection pools and data sources through server management work.


The following are the current popular WebLogic and Tomcat to illustrate

1.1 WebLogic Configuration
WebLogic must first configure the database connection pool, and then configure the data source, after the configuration is complete, config.xml in domain (here is MyServer) Add the following information:

<!--database connection pool configuration information-->
<jdbcconnectionpool
Drivername= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Name= "Myjdbcconnpool" passwordencrypted= "{3des}ieg3vohwdmo="

Properties= "USER=SA;URL=JDBC:MICROSOFT:SQLSERVER://DEMO:1433;SELECTMETHOD=CURSOR;DATASOURCENAME=SQL2000JDBC; Username=sa;datab

Asename=test;servername=demo "
targets= "MyServer" Testtablename= "SQL SELECT 1" url= "jdbc:microsoft:sqlserver://demo:1433"/>
<!--data source configuration information-->
<jdbctxdatasource jndiname= "Jndi_test" name= "Myjdbcdatasource"
Poolname= "Myjdbcconnpool" targets= "MyServer"/>

1.2 Tomcat Configuration
Tomcat is configured directly in the appropriate context (here is test, or in globalnamingresources, this configuration method please look for relevant information) data source, configure server configuration

When completed, the corresponding context configuration file is as follows:
<context docbase= "test" path= "/test" workdir= "Work/catalina/localhost/test" >
<resource name= "Hrmis" type= "Javax.sql.DataSource"/>
<resourceparams name= "Hrmis" >
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://demo:1433;DatabaseName=test</value>
</parameter>
<parameter>
<name>password</name>
<value>sa</value>
</parameter>
<!--defines the maximum number of database connections-->
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
</ResourceParams>
</Context>

2. Usage:

2.1 General Usage

You can create a database connection Manager to manage database connections as follows:

/*
* Date Created 2005-8-26
*
* Database Connection Manager V0.1
*
*/
Package com.brewx.db;

Import java.sql.Connection;
Import java.sql.SQLException;

Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;
Import Javax.sql.DataSource;

/**
* @author Jinghui

*
*/
Public final class Dbconnectionmanager {
Jndi Name
private static String Jndiname = "Jndi_test";

Whether it is a Tomcat server
private static Boolean istomcat = true;

Sync Lock
private static Object Initlock = new Object ();

 //data source, providing data connection
 private static DataSource DataSource = null;
&NBSP
 
 /**
  * Obtaining a database connection from the data source
  * @return connetion database connection
  * @throws SQLException
  * @throws namingexception
  */
 public static Connection getconnection () throws SQLException,
   namingexception {
  if (DataSource = = null) {
    Synchronized (initlock) {
    if (DataSource = = null) {
      Context ctx = new InitialContext ();

if (Istomcat) {
If Tomcat, find context Java:comp/env
CTX = (context) ctx.lookup ("java:comp/env");
}
Find a data source
DataSource = (dataSource) ctx.lookup (jndiname);
}
}

}
return Datasource.getconnection ();
}

}


2.2 Usage in Hibernate
For Tomcat configuration under Hibernate, refer to the Hibernate manual.
The WebLogic configuration is as follows:

<session-factory>
<!--Properties-->
<property name= "dialect" >net.sf.hibernate.dialect.SQLServerDialect</property>
<property name= "Connection.datasource" >JNDI_Test</property>

<!--mapping Files-->
<!--The following mapping element is auto-generated in-->
<!--conform to the Hibernate DTD-->
<mapping resource= "/com/brewx/ht/hibcfg/user.hbm.xml"/>
......

</session-factory>

3. Summary
This method configuration and usage are very simple, if the performance can pass (I do not test the performance:), is worth using.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.