JDBC Link Pool

Source: Internet
Author: User

Package cn.itcast.jdbc.datasourse;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.util.LinkedList;

public class Mydatasourse {
private static String URL = "Jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String pwd = "";

For storing links
Private linkedlist<connection> Connectionspool = new linkedlist<connection> ();

When you initialize a link pool, put 10 links into the pool of links
Public Mydatasourse () {
try {
for (int i = 0;i<; i++) {
This.connectionsPool.addLast (This.createconnection ());
}
} catch (SQLException e) {
E.printstacktrace ();
throw new Exceptionininitializererror (e);
}
}
Get links
Public Connection getconnection () {
return This.connectionsPool.removeFirst ();
}

Release link If we use the link pool, we can not simply shut down the conn when we want to release the resources, instead of putting the conn back into the link pool.
public void Free (Connection conn) {
THIS.CONNECTIONSPOOL.ADDLAST (conn);
}
Create a link
Private Connection CreateConnection () throws sqlexception{
Return drivermanager.getconnection (URL,USER,PWD);
}
}
/*
* When we are working on a database, we often spend the longest time building and database links, so it is best to link to ensure that links do not
* Frequent establishment, it should be considered to create a link pool, each time use directly from the link pool to fetch, run out and then put back
* This will take a long time to create, but once created, it's a good way to improve efficiency when you're done.
* */
At this time the Jdbcutils package should be changed to this////////////////////////////////////////////////////// ////////////////////

Package cn.itcast.jdbc;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import cn.itcast.jdbc.datasourse.MyDataSourse;

Public final class Jdbcutils {
    private static String url = "Jdbc:mysql://localhost:3306/test";
    private static String user = "root";
    private static String password = "";
  //private static Mydatasourse Mydatasourse = new Mydatasourse ();   // Here Mydatasourse cannot initialize here, because the Mydatasourse initialization action needs to be registered before the driver

private static Mydatasourse mydatasourse = null; It is only necessary to declare that the initialization action is placed behind the registration driver, otherwise the initialization exception will be reported.

Private method of construction
Private Jdbcutils () {
}

Put the registration-driven action inside the static code block
static {
try {
Class.forName ("Com.mysql.jdbc.Driver");

Mydatasourse = new Mydatasourse (); The initialization action is placed behind the registration driver


} catch (ClassNotFoundException e) {
E.printstacktrace ();
throw new Exceptionininitializererror (e);
}
}

Establish links
public static Connection getconnection () throws SQLException {
Return drivermanager.getconnection (URL, user, password);
return Mydatasourse.getconnection ();
}

Freeing resources
public static void Free (ResultSet rs, Statement St, Connection conn) {
try {
if (rs! = null) {
Rs.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (st! = null) {
St.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
IF (conn! = null) {
Conn.close (); When using a link pool, it is not possible to simply close the Conn.
Mydatasourse.free (conn);//You should put the link conn back in the link pool now
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}
}

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.