Several configuration methods of Java database connection pool (take MySQL database as example) _java

Source: Internet
Author: User
Tags auth connection pooling informix time interval

A. Tomcat configuration data Source:

Prerequisite: You need to place the MySQL database driver jar package into the Lib directory under the common folder in the Tomcat installation directory

1. Method One: Under the Webroot to build a folder Meta-inf, inside to build a file Context.xml, as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<Context> <resource name= "Jdbc/chaoshi" auth= "
Container "
type=" Javax.sql.DataSource "maxactive=" maxidle= "maxwait=" 10000 "
true"
username= "root" password= "root" driverclassname= "Com.mysql.jdbc.Driver"
url= "Jdbc:mysql://localhost" : 3306/dbname ">
</Resource>
</Context>

Method Two: In the context.xml under Tomcat's directory conf, modifies the original context label, changes to:

<Context> 
<!--Default set of monitored resources-->
<WatchedResource>WEB-INF/web.xml< /watchedresource> 
<resource name= "jdbc/test"
auth= "Container" type= "Javax.sql.DataSource" Maxactive= "maxidle=" maxwait= "10000" logabandoned= "true"
username= "root" password= "root
" Driverclassname= "Com.mysql.jdbc.Driver"
url= "Jdbc:mysql://localhost:3306/testdb"/>
</Context>

Mode three: When the virtual directory is configured, that is, when Server.xml is configured under Conf, it is changed in the Context tab:

<context path= "/webroot" reloadable= "true" 
docbase= "E:\workspace\DataSource\WebRoot" >
< Resource name= "Jdbc/test" auth= "Container" type= "Javax.sql.DataSource" maxactive= "maxidle=" "
30" Maxwait= "10000"
logabandoned= "true"
username= "root" password= "root"
driverclassname= " Com.mysql.jdbc.Driver "
url=" Jdbc:mysql://localhost:3306/testdb "/>

</the meaning of each property in the resource label in the configuration file:

The full name of the database-driven class used by the DRIVERCLASSNAME-JDBC.
Maxactive-The maximum number of active connections that the connection pool provides at the same time.
Maxidle-The maximum number of connections that the connection pool maintains at idle times.
Maxwait-The maximum number of milliseconds that the database waits when an exception occurs (when no connection is available).
Username-logon name when connecting to database
Password-the password to connect to the database.
URL-connects to the driver URL. (For backwards compatibility, drivername is also allowed.)

Test code:

Context Initcontext = new InitialContext ();
Context Envcontext = (context) initcontext.lookup ("java:/comp/env");
DataSource ds = (DataSource) envcontext.lookup ("Jdbc/test");
System.out.println (Ds.getconnection ());

Printing is not NULL should be successful.

Note that the test is to be tested in Tomcat, that is, in the Tomcat container (do not bother, write a simple JSP page test, with a <%...%> on it, quite simple). Not measured in the Tomcat container, the exception is thrown:

Javax.naming.NoInitialContextException:Need to specify class name in environment or system, or as a applet parameter, or in a application resource file:java.naming.factory.initial

Two, there are three ways to configure connection pooling hibernate:

Method 1 uses Hibernate's own connection pool.

 

Mode 2: Use the database connection pool specified by the configuration file.

Connection pool Now there are dbcp, C3P0, Proxoop, in fact, I knew DBCP

where dbcp, C3P0 configuration only need to add some configuration on the above configuration on the line, hibernate will automatically identify the database connection pool

Configuration dbcp need to join:

<!--dbcp conf
<property name= "dbcp.maxactive" >100</property>
<property name= " Dbcp.whenexhaustedaction ">1</property>
<property name=" dbcp.maxwait ">60000</property>
<property name= "Dbcp.maxidle" >10</property>
<property name= "Dbcp.ps.maxActive" >100 </property>
<property name= "dbcp.ps.whenExhaustedAction" >1</property>
<property Name= "dbcp.ps.maxWait" >60000</property>
<property name= "Dbcp.ps.maxIdle" >10</property>
-->

Configuration c3p0 need to join:

<!--c3p0 conf
<property name= "c3p0.min_size" >5</property>
<property name= "c3p0.max_size ">30</property>
<property name=" c3p0.time_out ">1800</property>
<property name=" C3p0.max_statement ">50</property>
-->

The configuration Proxoop is a little different, you can't just join, you need to change:

The basic configuration is as follows:

<property name= "Proxool.pool_alias" >dbpool</property>
<property name= "Proxool.xml" >test/ huatech/conf/proxoolconf.xml</property>
<property name= "Connection.provider_class" > Org.hibernate.connection.proxoolconnectionprovider</property>

Special Note: The following file path to be configured correctly, otherwise FileNotFound

Associated files: Test/huatech/conf/proxoolconf.xml configuration is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <something-else-entirely> <proxool> <alias>dbpool </alias> <!--Proxool can only manage connections generated by itself--> <driver-url> JDBC:INFORMIX-SQLI://192.168.0.188:1526/DB_CRM : INFORMIXSERVER=OL_SX; Newlocale=zh_cn,en_us;
newcodeset=gbk,8859_1,819; </driver-url> <driver-class>com.informix.jdbc.IfxDriver</driver-class> <driver-properties > <property name= "User" value= "Informix"/> <property name= "password" value= "Informix"/> </ driver-properties> <!--Proxool automatically detects the time interval (milliseconds) of each connection state, reclaims the idle connection immediately, and then destroys the-->  

Mode 3: Get the connection pool from the container (for example, Tomcat)

Using the server's own connection pool: such as Tomcat, resin, weblogic, etc.

The hibernate configuration is as follows:
<!--
<property name= "Hibernate.connection.datasource" >
java:comp/env/jdbc/ CRM
</property>
<property name= "Show_sql" >true</property>
<property name= " Dialect ">
com.huatech.sysframe.webapp.common.dao.hibernate.dialet.BaseInformixDialect
</property >
<property name= "Hibernate.generate_statistics" >true</property>
-->

Where the Java:comp/env/jdbc/crm JDBC/CRM is the corresponding server in the database connection pool name, needs to be configured in the corresponding environment

The tomcat configuration, as described in the first Tomcat configuration, notes that the name of the Jndi is modified to match the name used by the hibernate.
========================================
The above configuration requires the use of the respective database connection pool jar package, in the Hibernate package, if you need the latest can be downloaded to their respective websites.

Third, spring configures the connection pooling method:

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property Name= "Driverclassname" > <value>com.mysql.jdbc.Driver</value> </property> <property name= " URL "> <value>jdbc:mysql://localhost:3306/dbname</value> </property> <property name=" Username "> <value>root</value> </property> <property name=" password "> <value>***** *</value> </property> <property name= "maxactive" > <value>100</value> </property > <property name= "maxwait" > <value>1000</value> </property> <property name= "Maxidle" > <value>30</value> </property> <property name= "Defaultautocommit" > <value>true </value> </property> <property name= "removeabandoned" >//Automatically recycle connection pool to avoid leakage of connection pool <value>true</ value> </property> <property name= "Removeabandonedtimeout" > <value>60</vAlue> </property> </bean> 

Four, and the last one I'm going to say today, is to configure the connection pool by writing the code as follows:

 import java.sql.*; import Java.sql.DataSource; import
Org.apache.commons.dbcp.BasicDataSource; public class connectionpool{private static Basicdatasource DBS = NULL; public static DataSource Setupdatasource () {BDS =
New Basicdatasource ();
Set driver Bds.sestdriverclassname ("Com.mysql.jdbc.Driver");
Set connection user name Bds.setusername ("root");
Set the connection password Bds.setpassword ("root");
Set the connection address Bds.seturl ("Jdbc:mysql://localhost:3306/databasename");
Set the total number of initial connections bds.setinitialsize (50);
Set the total number of connections applied simultaneously bds.setmaxactive (-1);
Set the maximum number of connections in the buffer pool Bds.setmaxidle (-1);
The minimum connection number of the buffer pool is set Bds.setminidle (0);
Set the maximum wait Time bds.setmaxwait (-1);
Return (DataSource) BDS; ///Display Connection pool connection number method public static void Printdatasourcestats (DataSource ds) throws sqlexception{BDS = (basicdatasource) ds; S
Ystem.out.println ();
System.out.println (); ///Close connection pooling method public static void Shutdowndatasource (DataSource ds) throws sqlexception{BDS = (basicdatasource) ds; Bds.clos
E (); }
}

To obtain a connection pool only the static method Setupdatasource () that uses the class can be

The above is a small set of Java database Connection pool to introduce several configuration methods (to MySQL database for example), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.