At present, the vast majority of software systems use the database, and after the software is built, access to the database becomes the software system performance of the short board (I/O operations). In general, a database connection is required to access the database at one time. Each time you create a database connection, you need access, allocate idle resources, consume resources, release resources, and end access. It's very time-consuming and space-intensive.
So the data connection pool technology is produced, the principle is to establish a database connection management layer between the data requester and the database. Assign a certain number of connections when the system is started, and maintain these connections, keeping the minimum number of connections. At the same time, in order to protect the database and system, set a maximum number of connections, alleviate the database access pressure.
This allows the manager to assign a connection to the data requester each time the data requester needs to connect, and the data requester uses the connection to return the connection to the manager, thus minimizing unnecessary database connection consumption.
The Java Database service JDBC does not provide an actual connectionpool implementation, but leaves out an interface to support a third-party service provider, and I think the most important interface is datasource.
There are actually four ways that Java accesses a database mainly:
First, the use of Drivermananger to direct programming implementation, this is the most basic way.
Drivermananger key steps to implement a database connection are:
1>class.forname ("Com.ibm.db2.jcc.DB2Driver"). newinstance ();
2>connection conn = null;
3>conn = drivermanager.getconnection (URL, "name", "password");
Second, use the server configuration file to implement the connection pool.
Some servers provide their own database connection pooling services, such as the Tomcat server, which provides a data pool connection service supported by COMMONS-DBCP, just fill in the corresponding values in the context.xml.
Third, configuration file configuration DataSource
DataSource is an interface provided in the JAVAAPI, third-party services after the implementation of DataSource, such as the Basicdatasource of DBCP, in order to improve the configuration of the database connection pool, the use of IOC mode (control inversion), Separating the actual bean from the configuration data, the configuration data is placed in the configuration file (typically using XML). To implement control over multiple databases.
After the DataSource object is instantiated, it can be connected via datasource.getconnection ().
Iv. connecting the bridge by configuring the JDBC-ODBC
Have heard, have not used, have no say.
Use the COMMONS-DBCP provided by Apache here to implement a separate, simple database connection pool.
First, you need three kits: Commons-pool.jar,commons-collection.jar,commons-dbcp.jar.
The key implementation snippet is shown below:
Java code
- Holds a static database connection pool Object
- Private static DataSource DS;
- Implement DataSource interface with Basicdatasource provided by DBCP
- Public static void InitDataSource (String Connecturi, string username,
- string password, string driverclass, int initialsize,
- int maxactive, int maxidle, int maxwait) {
- Basicdatasource ds = new Basicdatasource ();
- Ds.setdriverclassname (Driverclass);
- Ds.setusername (username);
- Ds.setpassword (password);
- Ds.seturl (Connecturi);
- Ds.setinitialsize (initialsize);
- Ds.setmaxactive (maxactive);
- Ds.setmaxidle (Maxidle);
- Ds.setmaxwait (maxwait);
- ds = ds;
- }
- Get a database connection
- Public Connection getconnection () {
- Connection con = null;
- if (DS! = null) {
- try {
- con = ds.getconnection ();
- } catch (Exception e) {
- System.out.println (E.getmessage ()); }
- //Set the database connection to not default to automatic commit
- try {
- Con.setautocommit (false);
- } catch (SQLException e) {
- System.out.println (E.getmessage ()); }
- return con;
- }
- //When retrieving database connections, use Con.close () directly
- return con;
- }
- Recycle database connections
- protected static void Shutdowndatasource () throws SQLException {
- Basicdatasource BDS = (basicdatasource) DS;
- Bds.close ();
- }
The above code is extracted from the actual use of code, I hope to help you.
At present, OpenSource provides a lot of database connection pool technology, the use of processes and patterns, and the above code is similar. At the same time the latest release of BONECP I have never felt, I hope to have friends to share with me the use of experience.
- Commons-collections-3.2.1.jar (561.9 KB)
- Download number of times: 73
- Commons-pool-1.5.5.jar (97.8 KB)
- Download number of times: 55
- Commons-dbcp-1.4.jar (156.8 KB)
- Download number of times: 59
Java uses a standalone database connection pool (dbcp as an example)