Common Tomcat data source configuration

Source: Internet
Author: User
Tags informix

1. Tomcat data source Configuration:

 

Method 1: Create a folder META-INF under webroot, which creates a file context. XML, the content is as follows:

<Context>

<Resource Name = "JDBC/test" auth = "Container" type = "javax. SQL. datasource"

Maxactive = "50" maxidle = "30" maxwait = "10000" logabandoned = "true"

Username = "root" Password = "111111" driverclassname = "com. MySQL. JDBC. Driver"

Url = "JDBC: mysql: // localhost: 3306/testdb"/>

</Context>

 

Method 2: In the context. xml file under the conf directory of tomcat6.0, modify the original context label to the following content:

<Context>

 

<! -- Default set of monitored resources -->

<Watchedresource> WEB-INF/Web. xml </watchedresource>

 

<Resource Name = "JDBC/test" auth = "Container" type = "javax. SQL. datasource"

Maxactive = "50" maxidle = "30" maxwait = "10000" logabandoned = "true"

Username = "root" Password = "111111" driverclassname = "com. MySQL. JDBC. Driver"

Url = "JDBC: mysql: // localhost: 3306/testdb"/>

 

</Context>

 

Method 3: When configuring the virtual directory, that is, When configuring server. xml under the conf file, change it to the following form in the context Tag:

<Context Path = "/webroot" reloadable = "true" docbase = "E: \ workspace \ datasource \ webroot">

<Resource Name = "JDBC/test" auth = "Container" type = "javax. SQL. datasource"

Maxactive = "50" maxidle = "30" maxwait = "10000" logabandoned = "true"

Username = "root" Password = "111111" driverclassname = "com. MySQL. JDBC. Driver"

Url = "JDBC: mysql: // localhost: 3306/testdb"/>

</Context>

 

Meanings of attributes in the resource tag in the configuration file:

 

Full name of the database Driver Class used by driverclassname-JDBC.

 

Maxactive-Maximum number of active connections provided by the connection pool at the same time.

 

Maxidle-Maximum number of connections that the connection pool maintains during idle time.

 

Maxwait-Maximum number of milliseconds the database waits when an exception occurs (when no available connection is available ).

 

Password-password used to connect to the database.

 

URL-connect to the driver URL. (For backward compatibility, drivername is also allowed .)

 

User-database username.

 

The scope of various configuration methods should also be different. I will not elaborate here. In short, configure a resource tag under the context tag.

 

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 ());

It should be successful if it is not null.

Note: during testing, you need to test in Tomcat, that is, in the Tomcat container (do not worry, write a simple JSP page for testing, and use a <%... %> it is quite simple ). If it is not tested in the Tomcat container, an exception is thrown:

 

... Javax. Naming. noinitialcontextexception: need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: Java. Naming. Factory. Initial

 

2. There are three methods for configuring the connection pool in hibernate:

 

Method 1 use the connection pool that comes with hibernate.

 

<Hibernate-configuration>

<Session-factory>

<! -- JDBC driver -->

<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>

<! -- Connection URL -->

<Property name = "connection. url">

JDBC: mysql: // localhost: 3306/Feifei

</Property>

<! -- Connection login name -->

<Property name = "connection. username"> root </property>

<! -- Logon password -->

<Property name = "connection. Password"> </property>

<! -- Whether to output SQL statements generated during runtime to logs for debugging -->

<Property name = "show_ SQL"> true </property>

<! -- Specify the connection language -->

<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property>

<! -- Map resources -->

<Mapping Resource = "/XX. HBM. xml"/>

</Session-factory>

</Hibernate-configuration>

<Session-factory>

<Property name = "show_ SQL"> true </property>

<! -- Common conf DBCP/c3p0 needed

<Property name = "connection. username"> Informix </property>

<Property name = "connection. Password"> Informix </property>

<Property name = "connection. driver_class"> com. Informix. JDBC. ifxdriver </property>

<Property name = "connection. url">

JDBC: Informix-sqli: // 192.168.0.188: 1526/db_crm: informixserver = ol_sx; newlocale = zh_cn, en_us; newcodeset = GBK, 8859_1, 819;

</Property>

<Property name = "dialect">

Com. huatech. sysframe. webapp. Common. Dao. hibernate. dialet. baseinformixdialect

</Property>

--> </Session-factory>

</Hibernate-configuration>

 

========================================================== ========================================================== =

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

The connection pool now includes DBCP, c3p0, and proxoop. In fact, I knew DBCP

The configuration of DBCP and c3p0 only needs to be added to the above configuration. hibernate will automatically identify the database connection pool.

 

DBCP configuration needs to be added:

<! -- 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>

-->

To configure c3p0, add:

<! -- 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 of proxoop is somewhat different and cannot be added only. You also need to modify it: <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>

 

Note: the path of the following files must be correctly configured; otherwise, filenotfound

Associated file: Test/huatech/CONF/proxoolconf. XML is configured 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>

<! -- The proxool automatically detects the time interval (in milliseconds) of each connection status. When idle connections are detected, the system immediately recycles the connection status and destroys the connection timeout. -->

<House-keeping-sleep-time> 90000

<! -- Indicates the maximum number of requests waiting in the queue because no idle connections can be allocated. User connections exceeding the number of requests will not be accepted. -->

<Maximum-New-connections> 20 </maximum-New-connections>

<! -- Minimum number of idle connections maintained -->

<Prototype-count> 5 </prototype-count>

<! -- The maximum number of connections allowed. If the connection is exceeded, the requests are placed in the queue. the maximum number of waiting requests is determined by maximum-New-connections. -->

<Maximum-connection-count> 100 </maximum-connection-count>

<! -- Minimum connections -->

<Minimum-connection-count> 10 </minimum-connection-count>

</Proxool>

</Something-else-entirely>

 

========================================================== ========================================================== =

 

Method 3: Obtain the connection pool from the container (for example, Tomcat)

Use the connection pool of the server itself: 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>

-->

The JDBC/CRM of Java: COMP/ENV/jdbc/CRM is the name of the database connection pool in the corresponding server, which must be configured in the corresponding environment.

 

Tomcat configuration is described in the First tomcat configuration method. Note that the JNDI name should be modified according to the situation and correspond to the name used by hibernate.

 

========================================================== ========================================================== =

The jar package of the respective database connection pool is used in the above configuration. It is included in the hibernate package. If you need the latest package, you can download it from your own website.

 

3. Configure the connection pool using spring:

 

<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 recycles the connection pool to avoid leakage of the connection pool.

<Value> true </value>

</Property>

<Property name = "removeabandonedtimeout">

<Value> 60 </value>

</Property>

 

</Bean>

 

4. The last one I want to talk about today is to configure the connection pool by writing code. The Code is 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 the driver

 

BDS. sestdriverclassname ("com. MySQL. JDBC. Driver ");

 

// Set the 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 initialized connections

 

BDS. setinitialsize (50 );

 

// Set the total number of connections simultaneously applied

 

BDS. setmaxactive (-1 );

 

// Set the maximum number of connections in the buffer pool

 

BDS. setmaxidle (-1 );

 

// Set the minimum number of connections in the buffer pool

 

BDS. setminidle (0 );

 

// Set the longest waiting time

 

BDS. setmaxwait (-1 );

 

Return (datasource) BDS;

 

}

 

// Display the number of connections in the connection pool

 

Public static void printdatasourcestats (datasource DS) throws sqlexception {

 

BDS = (basicdatasource) ds;

 

System. Out. println ();

 

System. Out. println ();

 

}

 

// Method for closing the connection pool

 

Public static void shutdowndatasource (datasource DS) throws sqlexception {

 

BDS = (basicdatasource) ds;

 

BDS. Close ();

 

}

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.