These days in learning to use the MVC pattern to do a few small projects, in the course of learning, the use of database connection pool. I went to study for a moment.
First, talk about why to use database connection pool
In the development of database-based web programs, there are some problems with traditional schemas (establishing database links in Servlet,beans, doing SQL operations, disconnecting database links).
Each connection needs to authenticate the user, consuming a lot of resources and time. The database's connection resources are not being reused well. Another example, tens of thousands of people online frequent connection database, system resource consumption is huge, memory may leak, the server may also crash.
Ii. What is a database connection pool
1. Basic idea : You can create a "buffer pool" for the database. Pre-placed a certain number of connections in the buffer pool, when the need to establish a database connection, only need to remove one from the "buffer pool", and then put back to use, to avoid the time and resources of repeated connections.
2. Responsibilities : Database connection pooling is responsible for assigning, managing, and releasing database links, which allows applications to reuse an existing database link instead of creating one.
The 3.JDBC database link pool uses Javax.sql.DataSource to represent that DataSource is just an interface that is typically implemented by the server (webblogic,tomact), and some open source organizations provide the implementation: DBCP,C3P0.
Third, using the C3P0 database connection pool
There are two ways to connect c3p0.
1. The first type:
Specific steps:
1. Import the required Jar packages
2.java Code
1 Importjava.beans.PropertyVetoException;2 Importjava.sql.Connection;3 Importjava.sql.SQLException;4 5 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;6 7 8 Public classJdbcutils {9 Private StaticConnection conn=NULL;Ten Private StaticCombopooleddatasource combopooleddatasource=NewCombopooleddatasource (); One A Public StaticConnection getconnection () { - Try { -Combopooleddatasource.setdriverclass ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); theCombopooleddatasource.setjdbcurl ("Jdbc:sqlserver://localhost:1433;databasename=mysql"); -Combopooleddatasource.setuser ("sa"); -Combopooleddatasource.setpassword ("1546873"); - +Combopooleddatasource.setacquireincrement (5);//you can set various properties of a connection pool - +conn=combopooleddatasource.getconnection (); A}Catch(propertyvetoexception e) { at //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(SQLException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } in returnConn; - } to +}
2. The second type:
Specific steps:
1. Importing the JAR Package
2. Configure the XML file. The profile name must be named C3p0-config.xml and placed in the SRC directory
The code is as follows
1 <C3p0-config> 2 <Named-configname= "Myc3p0"> 3 < Propertyname= "User">Sa</ Property> 4 < Propertyname= "Password">ztg591379771</ Property> 5 < Propertyname= "Driverclass">Com.microsoft.sqlserver.jdbc.SQLServerDriver</ Property> 6 < Propertyname= "Jdbcurl">Jdbc:sqlserver://localhost:1433;databasename=mysql</ Property> 7 8 <!--How many links are requested to the database server at a time if the number of connections in the database is insufficient -9 <!--when the connection in the connection pool runs out, c3p0 the number of connections that are fetched at one time. Default:3 -Ten < Propertyname= "Acquireincrement">5</ Property> One A - <!--when initializing, get three connections, and the value should be between Minpoolsize and Maxpoolsize. Default:3 - - < Propertyname= "Initialpoolsize">10</ Property> the - - <!--the minimum number of connections that are kept in the connection pool. - - < Propertyname= "Minpoolsize">10</ Property> + - + <!--the maximum number of connections that are kept in the connection pool. Default:15 - A < Propertyname= "Maxpoolsize">50</ Property> <!--Intergalactoapp adopts a different approach to configuring statement caching - at - <!--the standard parameters of JDBC to control the number of preparedstatements loaded within the data source. But due to the statements of the pre-cache - belong to a single connection instead of the entire connection pool. So setting this parameter takes into account a variety of factors. - if both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0 - - < Propertyname= "Maxstatements">20</ Property> - in - <!--Maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. default:0 - to <!--The number of statements objects that can be used per link - + < Propertyname= "Maxstatementsperconnection">5</ Property> <!--he ' s important, but there 's only one of him - - the </Named-config> * </C3p0-config>
3. Writing the Utils tool class
1 Importjava.sql.Connection;2 Importjava.sql.SQLException;3 4 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;5 6 Public classJdbcutils {7 8 /**9 * Release LinkTen * @paramConnection One */ A Public Static voidreleaseconnection (Connection Connection) { - if(connection!=NULL){ - Try { the connection.close (); -}Catch(SQLException e) { - //TODO auto-generated Catch block - e.printstacktrace (); + } - } + } A at - Private StaticCombopooleddatasource datasource=NULL; - - Static{ - //is created only once -Datasource=NewCombopooleddatasource ("Myc3p0"); myc3p0 must be the same as the name in the configuration file in } - to /** + * Returns a connection object for the data source - * @return the * @throwsException * */ $ Public StaticConnection getconnection ()throwsexception{Panax Notoginseng returndatasource.getconnection (); - } the}
The above is what I learned today about c3p0 some of the knowledge, the future in the process of learning slowly groping, gradually supplemented.
The use of C3P0 in Javaweb learning process