1.Tomcat configuration data source:
Prerequisite: You need to put the connected MySQL database driver jar package into the lib directory under the common folder in the Tomcat installation directory.
1. Method 1: Create a folder META-INF under WebRoot, and create a file context.xml in it, as follows:
<?xml version="1.0" encoding="UTF-8"?><Context><Resource name="jdbc/chaoshi" auth="Container"type="javax.sql.DataSource"maxActive="50" maxIdle="30" maxWait="10000" logAbandoned="true"username="root" password="root" driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/DBName" ></Resource></Context>
Method 2: In the context.xml below the tomcat directory conf, modify the original context tag and change it to:
<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="root"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/testdb" /></Context>
Method 3: When configuring the virtual directory, that is, when configuring the server.xml below conf, change it to 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="root"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/testdb" /></Context>
</The meaning of each attribute in the Resource tag in the configuration file:
driverClassName - The full name of the database driver class used by JDBC.
maxActive - The maximum number of active connections provided by the connection pool at the same time.
maxIdle - The maximum number of connections that the connection pool holds during idle time.
maxWait - The maximum number of milliseconds the database waits when an exception occurs (when there is no available connection).
username - Login name when connecting to the database
password - The password to connect to the database.
url - URL to the driver. (DRIVERNAME is also allowed for backward compatibility.)
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());If it is not null, it should be successful.
Note that when testing, you must test it in tomcat, that is, it must be in the TOMCAT container (don't bother, write a simple JSP page to test it, and use <%...%>, which is quite simple). If it is not tested in the tomcat container, an exception will be 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 ways to configure connection pools in Hibernate:
Method 1 Use Hibernate's own connection pool.
<hibernate-configuration><session-factory ><!--JDBC driver--><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- URL to connect to the database--><property name="connection.url">jdbc:mysql://localhost:3306/feifei</property><!--Login name for the connection--><property name="connection.username">root</property><!--Login Password--><property name="connection.password"></property><!--Whether to output the SQL generated during the run to the log for debugging--><property name="show_sql">true</property><!--Specify the language for connection--><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!--Mapping resources--><mapping resource="/xx/xx.hbm.xml" /></session-factory></hibernate-configuration>My own configuration is as follows, the commented part is public, and other connection pools need to be used! <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 has dbcp, c3p0, and proxoop. Actually, I knew dbcp
Among them, the configuration of dbcp and c3p0 only needs to add some configurations to the above configuration, and hibernate will automatically identify the database connection pool.
To configure dbcp, you 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.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, you 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 of proxoop is a bit different. You cannot just add it, but you need to change it:
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 path of the following file must be configured correctly, otherwise FileNotFound
The 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><!-- proxool automatically detects the time interval (milliseconds) of each connection state. If the idle connection is detected, it will be recycled immediately. Timeout destruction--><house-keeping-sleep-time>90000</house-keeping-sleep-time><!-- It refers to the maximum number of requests waiting in the queue because there is no idle connection to be allocated. User connections that exceed this number of requests will not be accepted--><maximum-new-connections>20</maximum-new-connections><!-- The minimum number of idle connections maintained--><prototype-count>5</prototype-count><!-- The maximum number of connections allowed is exceeded. When there is a request, it is queued to wait in the queue. The maximum number of waiting requests is determined by maximum-new-connections--><maximum-connection-count>100</maximum-connection-count><!-- The minimum number of connections--><minimum-connection-count>10</minimum-connection-count></proxool></something-else-entirely>
Method 3: Get the connection pool from the container (such as Tomcat)
Use the server's own connection pool: such as Tomcat, resin, weblogic, etc.
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>-->
Among them, jdbc/crm of java:comp/env/jdbc/crm is the name of the database connection pool in the corresponding server, and needs to be configured in the corresponding environment.
Tomcat configuration is described in the first Tomcat configuration method. Note that the name of jndi should be modified according to the situation and should correspond to the name used by hibernate.
=====================================================
The above configuration requires the jar package for each database connection pool, which is included in the hibernate package. If you need the latest one, you can download it from your respective websites.
3. Spring's method of configuring connection pool:
<bean id="dataSource" 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 the connection pool to avoid leakage of the connection pool<value>true</value> </property></bean>
Fourth, which is also the last one I want to talk about today, is to configure the connection pool through code writing, 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 username bds.setUsername("root");//Set the connection password bds.setPassword("root");//Set the connection address bds.setUrl("jdbc:mysql://localhost:330 6/databasename");//Set the total number of initialized 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);//Set the minimum number of connections in the buffer pool bds.setMinIdle(0);//Set the longest waiting time bds.setMaxWait(-1); return (DataSource)bds;}//Method to 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 to close the connection pool public static void shutdownDataSource(DataSource ds) throws SQLException{bds = (BasicDataSource)ds;bds.close();}} To obtain a connection pool, you can only use the static method of this class to configureDataSource().
The above are several configuration methods for Java database connection pools introduced to you (taking MySQL database as an example). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!