When we are using Java to connect to the database, we want to register the driver. Then get the connection object through the "Driver Manager" (Connection), when our program is accessed by many users, each time we create a Connection connection object, the program will become a special card, At this point we can solve this problem through the database connection pool.
Use connection pooling to manage connection, which can be reused with connection. With the pool, we don't have to create connection ourselves, but instead we get the connection object through the pool. When connection is finished, calling connection's close () method does not actually close the connection, but instead connection "returns" to the pool. The pool will be able to use this connection object again.
Common Connection Pools:
1). DBCP Connection pool: Apache Company's free, open source connection pool implementation. Inside Apache's Tomcat server is the DBCP connection pool used.
2). C3P0 Connection pool: free, open source. In a long-running situation, the C3P0 internal resource release function is more powerful than DBCP. World Mainstream framework: Spring Framework, Hibernate framework Interior
The C3P0 connection pool is used.
In accordance with this principle, I wrote a simple database connection pool, not considered very comprehensive, for reference only, interested friends can define a more logical connection pool.
First write a configuration file that writes information about the connection database to the configuration file:
1 driverclassname=com.mysql.jdbc.Driver2 url=jdbc:mysql://localhost:3306 /student3 username=root4 password=123456
Custom database Connection Pool code:
1 PackageConnectionPool;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.SQLException;6 Importjava.util.LinkedList;7 Importjava.util.Properties;8 9 ImportJavax.sql.DataSource;Ten One //Create a custom database connection pool; A Public classjdbcpool{ - //the Connection object is stored by LinkedList collection to simulate the reuse process of Onnection object . - //initializes the number of connection objects; the //define the maximum connection object; - //define the minimum number of objects to be reserved for a connection pool - Private StaticLinkedlist<connection>link; - Private StaticProperties p; + Private Static intInitcount=5; - Private Static intmaxcount=10; + Private Static intMincount=1; A //The registration drive is realized by the static code block; at Static{ -p=NewProperties (); - Try { -P.load (Jdbcpool.class. getClassLoader (). getResourceAsStream ("Dpconfig.properties")); -Class.forName (P.getproperty ("Driverclassname")); -link =NewLinkedlist<connection>(); in}Catch(Exception e) { - Throw Newruntimeexception (); to } + - } the //The number of initialized connection objects is implemented by constructing the method . * //I am here to initialize the connection object is 5; $ PublicJdbcpool () {Panax Notoginseng for(inti = 0; I <initCount; i++) { - Try { the This. Link.addfirst ( This. CreateConnection ()); +}Catch(SQLException e) { A the e.printstacktrace (); + } - } $ } $ //the method of creating a Connection object, if it is greater than the smallest created connection object, is taken out directly; - //if not, determine whether to reach the maximum number of connections, do not arrive, create a; - //I did not deal with the two situations do not meet, interested friends can try; the PrivateConnection getconnetion () { - synchronized(link) {if(Link.size () >Mincount) {Wuyi returnLink.removefirst (); the}if(Link.size () <MaxCount) { - Try { Wu return This. CreateConnection (); -}Catch(SQLException e) { About $ e.printstacktrace (); - } - } - return NULL; A } + } the //that is used to create the connection object. - PublicConnection CreateConnection ()throwssqlexception{ $ returnDrivermanager.getconnection (p.getproperty ("url"), P.getproperty ("username"), P.getproperty ("Password")); the } the //connection is retrieved to the collection when it is recycled, not closed the Public voidFree (Connection conn) { the This. LINK.ADDLAST (conn); - } in}
java-Customizing a simple MySQL database connection pool