Database connection pool: It is plainly in a pool (container) put a lot of database connections, when the user needs to take out a use, and then put back into the connection pool.
Advantages: Greatly improve the efficiency of the database.
For a custom database connection pool We use a linkedlist to do the database connection pool. This collection is characterized by the deletion of fast, query slow.
To customize a database connection pool:
1. Customize a class to implement a DataSource interface.
2. Define a list<connection> list=new linkedlist<connection> (); Store the database connection.
3. Initialize the database connection pool. is to add a number of connections to the list (the connection pool) to the static block of code blocks in which the database is added.
4. Write a method Returnpool (Connection con) also returns to the database connection pool. If no data connections are available in the database connection pool, add several database connection pools in the list.
5. In the Getconnection () method, the dynamic agent re-connection the Close method, because we can not close the end, but should be exhausted back to the database connection pool. This can be used in cycles.
Case:
Customizing your own database connection pool Mypool
Package Com.itheima.mypools;import Java.io.printwriter;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Java.sql.connection;import Java.sql.DriverManager; Import Java.sql.sqlexception;import Java.sql.sqlfeaturenotsupportedexception;import Java.util.LinkedList;import Java.util.list;import Java.util.logging.logger;import Javax.sql.datasource;public class MyPool implements DataSource {//1. Requires a container to store connection private static list<connection> list=new linkedlist<connection> ();// When the load class is initialized, 5 connections are added to the connection pool Static{try {//2. Load driver Class.forName ("Com.mysql.jdbc.Driver"); for (int i=0;i<5;i++) {//3. Get the Connection Connection con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day11", "root", "169500"); List.add ( con);}} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();}} @Overridepublic Connection getconnection () throws SQLException {//If the data for the connection pool is not added, add 3 connections to the connection pool if (list.size () ==0) {for (int i=0;i<3;i++) {Connection Con=drivermanager. getconnection ("Jdbc:mysql://localhost:3306/day11", "root", "169500"); List.add (con);}} To get the connection, remove the connection from the connection pool is not get...final Connection con = list.remove (0);//need to rewrite the close () method of the connection, we use dynamic proxy Connection proxy= ( Connection) Proxy.newproxyinstance (Con.getclass (). getClassLoader (), Con.getclass (). Getinterfaces (), New Invocationhandler () {@Overridepublic object Invoke (Object proxy, Method method, object[] args)//proxy proxy class objects method is represented by the methods of the class, args is the parameter of the proxy class method throws Throwable {if ("Close". Equals (Method.getname ())) {//the method to be reformed will also return to the connection pool returnpool (con); return null;} else{//does not want to transform the method of using the proxy object to return Method.invoke (Con, args);}}); System.out.println ("Gets a connection, the connection pool also has:" +list.size () + "connections"); return proxy;} public void Returnpool (Connection con) {try {if (con!=null&&!con.isclosed ()) {list.add (con);}} catch ( SQLException e) {e.printstacktrace ();} System.out.println ("Also back a connection, the pool is still remaining" +list.size () + "connections");} @Overridepublic PrintWriter Getlogwriter () throws SQLException {//TODO auto-generated method Stubreturn null;} @Overridepublic void Setlogwriter (PrintWriter out) throws SQLException {//TODO auto-generated method stub} @Overridepublic void Setlogintimeo UT (int seconds) throws SQLException {//TODO auto-generated method stub} @Overridepublic int Getlogintimeout () throws Sqlex ception {//TODO auto-generated method Stubreturn 0;} @Overridepublic Logger Getparentlogger () throws Sqlfeaturenotsupportedexception {//TODO auto-generated method Stubreturn null;} @Overridepublic <T> T Unwrap (class<t> iface) throws SQLException {//TODO auto-generated method Stubreturn nul l;} @Overridepublic boolean iswrapperfor (class<?> iface) throws SQLException {//TODO auto-generated method Stubreturn false;} @Overridepublic Connection getconnection (string Username, string password) throws SQLException {//TODO auto-generated Method Stubreturn null;}}
Use this self-defined database connection pool:
Package Com.itheima.mypools;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.ResultSet Import Java.sql.sqlexception;import Cn.itheima.utils.jdbcutils;public class JDBCDemo1 {public static void main (String [] args) {Connection con=null; PreparedStatement Ps=null; ResultSet Rs=null;try {//Use database connection pool Mypool pool=new mypool ();//Get Connection con=pool.getconnection ();p s=con.preparestatement (" SELECT * from Account "), Rs=ps.executequery (); while (Rs.next ()) {String name=rs.getstring (" name "); SYSTEM.OUT.PRINTLN (name);}} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} finally{//here the close has been more than the IF (rs!=null) {try {rs.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{rs=null;}} if (ps!=null) {try {ps.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{ps=null;}} if (con!=null) {try {con.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{con=null;}}}}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse Day11 Dynamic Agent & Simulation A database connection pool