JDBC (Connection Pool)--(I)

Source: Internet
Author: User
Tags connection pooling

Custom connection Pooling one:    The 1.sun provides a connection pool interface Javax.sql.DataSource.    2. Define the connection pool myDataSource Implement Interface DataSource.    3. Using the collection LinkedList < Connection > Pool store multiple Connection objects. linkedlist Remove add operation is highly efficient. The connection object also invokes the getconnection in the tool class Jdbcutils as before. (first introduction to JDBC)    4. Write the method to get the connection getconnection (), get the connection instead of taking the object from the pool.    5. Write the method Backconnection () to return the connection. Return the objects obtained from the pool pool.    We need three of classes:
1.JDBCUtils (the first JDBC essay has an introduction to how to write)
2.MyDataSource
3.TestMyDataSource

Note: The junit used for testing.
First, the writing myDataSource method, here does not implement the interface datasource
 PackageCom.it.JDBC;Importjava.sql.Connection;Importjava.util.LinkedList;/** * @authorpayphone * @time 2017-10-07 custom connection pool, no enhanced Close method **/ Public classmyDataSource {Private StaticConnection Conn; Private StaticLinkedlist<connection> pool =NewLinkedlist<connection>(); /*** Construction Method initializes the number of connection pools, and can also be initialized with static blocks of code. * */     PublicmyDataSource () { for(inti = 0; I < 5; i++) {Conn=jdbcutils.getconnection ();        POOL.ADD (conn); } System.out.println ("---------Split Line---------"); }    /*** Get Connections **/     PublicConnection getconnection () {System.out.println ("Number of connections before:" +pool.size ()); Conn= Pool.remove (0); System.out.println ("Number of connections:" +pool.size ()); returnConn; }    /*** Return connection **/     Public voidbackconnection () {System.out.println ("Number of releases before connection:" +pool.size ());        POOL.ADD (conn); System.out.println ("Number of free connections:" +pool.size ()); }}

Second, the writing test method Testmydatasource

Package Com.it.TestJDBC;

Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Org.junit.Test;

Import Com.it.JDBC.JDBCUtils;
Import Com.it.JDBC.MyDataSource;

public class Testmydatasource {

@Test
public void Testadduser () {
Connection conn = null;
myDataSource MD = new myDataSource ();
PreparedStatement psmt = null;
try {
conn = Md.getconnection ();
String sql = "INSERT into T_user (ID,PWD) VALUES (?,?)";
PSMT = conn.preparestatement (sql);
Psmt.setint (1, 2015005);
Psmt.setstring (2, "2015005");
int rows = Psmt.executeupdate ();
if (Rows > 0) {
System.out.println ("Insert data successfully! ");
} else {
System.out.println ("Insert data failed! ");
}
} catch (Exception e) {
throw new RuntimeException (e);
} finally {
Jdbcutils.release (NULL, PSMT, NULL);
Md.backconnection ();
}
}
}

Custom connection pooling A code is not perfect, we call the tool class Jdbcutils.release method to release the object except Conn. The Md.backconnection method is also called to return the Conn.

In the code for custom connection Pool Two we will enhance the Close method. Call the tool class Jdbcutils.release method to return the connection.

JDBC (Connection Pool)--(I)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.