Java--JDBC database connection pool
Blog description
The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!
concept
In fact, it is a container (Collection) for database connection.
After the system is initialized, the container is created and some connection objects are applied in the container. When the user comes to access the database, the connection objects are obtained from the container. After the user accesses the database, the connection objects are returned to the container.
benefit
save resources
Efficient user access
realization
Standard interface: datasource javax.sql Under the bag
- Get connection: getConnection()
- Return connection: Connection.close(). If the connection object connection is obtained from the connection pool, the Connection.close() method, the connection will no longer be closed. It's a return connection
Generally, we don't implement it. We have database manufacturers to implement it
C3P0
Database connection pool technology
Step:
-
Import the jar package (two) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar,
- Don't forget to import the database driver jar package
-
Define profile:
- Name: c3p0.properties or c3p0-config.xml
- Path: directly put the file in the src directory.
-
Create core object database connection pool object ComboPooledDataSource
-
Get connection: getConnection
code
//1. Create database connection pool object DataSource ds = new ComboPooledDataSource(); //2. Get connection object Connection conn = ds.getConnection();
Druid
Database connection pool implementation technology, provided by Alibaba
step
- Import the jar package druid-1.0.9.jar
- Define profile:
- It's in the form of properties
- It can be called any name and placed in any directory
- Load the configuration file. Properties
- Get database connection pool object: get DruidDataSourceFactory through factory
- Get connection: getConnection
code
//3. Load configuration file Properties pro = new Properties(); InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); //4. Get connection pool object DataSource ds = DruidDataSourceFactory.createDataSource(pro); //5. Get connection Connection conn = ds.getConnection();
Define tool class
- Define a class of JDBC utils
- Provide static code block to load configuration file and initialize connection pool object
- Provide methods
- Get connection method: get connection through database connection pool
- Release resources
- How to get the connection pool
- code
public class JDBCUtils { //1. Define the member variable DataSource private static DataSource ds; static { try { //1. Load configuration file Properties pro = new Properties(); pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); //2. Get DataSource ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * Get connection */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } /** * Release resources */ public static void close(Statement stmt, Connection conn) { close(null, stmt, conn); } public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close();//Return connection } catch (SQLException e) { e.printStackTrace(); } } } /** * Get connection pool method */ public static DataSource getDataSource() { return ds; } }
thank
Black horse programmer
And the industrious self
Pay attention to the official account: get back to the information, get more information, and have a longer learning plan.