Java learning notes-implementation of standard connection pool (27), java learning notes

Source: Internet
Author: User

Java learning notes-implementation of standard connection pool (27), java learning notes

Javax. SQL. DataSource.

Java. SQL .*

DataSourceThe interface is implemented by the driver supplier. There are three types of implementations:

  1. Basic implementation-generate a standard Connection object-only one Connection exists in a DataSource data source, which is not managed by the pool.
  2. Connection Pool implementation [W1]-generate a Connection object that automatically participates in the Connection pool.
  3. Distributed Transaction implementation-generate a Connection object, which can be used for distributed transactions and is always involved in the Connection pool in most cases. JTA. jar-SUN.

 

Standard connection pool, requirements:

1: implement the dataSource interface.

2: declare a collection class to manage multiple connections.

3: You must have the ability to recycle connections.

4: You must implement a method. getConnection gets a connection.

5: DataSource interface class, generally cannot have static pool object. List.

6: In a program, only one DataSource instance is required.

The specific implementation is as follows:

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. util. export list; import javax. SQL. dataSource;/*** standard Connection */public class DBPool implements DataSource {// declare a pool management object private writable list <Connection> pool = new writable list <Connection> (); // when initializing the subclass of Objective C, set multiple connections in the constructor public DBPool () {try {Class. forName ("com. mysql. jdbc. driver "); String url =" jdbc: mysql :/ // Db909? CharacterEncoding = UTf8 "; for (int I = 0; I <3; I ++) {Connection con = DriverManager. getConnection (url, "root", "1234"); // The generated connection. Put it in the pool. add (con) ;}} catch (Exception e) {throw new RuntimeException (e. getMessage (), e) ;}} public Connection getConnection () throws SQLException {return pool. removeFirst ();} // other methods. Not implemented}

2: reclaim connections

Package cn. itcast. utils; 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. util. export list; import javax. SQL. dataSource;/*** standard Connection */public class DBPool implements DataSource {// declare a pool management object private writable list <Connection> p Ool = new consumer list <Connection> (); // when initializing this consumer C subclass, set multiple connections in the constructor public DBPool () {try {Class. forName ("com. mysql. jdbc. driver "); String url =" jdbc: mysql: // db909? CharacterEncoding = UTf8 "; for (int I = 0; I <3; I ++) {final Connection con = DriverManager. getConnection (url, "root", "1234"); Object proxyedConn = Proxy. newProxyInstance (DBPool. class. getClassLoader (), new Class [] {Connection. class}, new InvocationHandler () {public Object invoke (Object proxyedConnection, Method method, Object [] args) throws Throwable {if (method. getName (). equals ("close") {synchronized (pool) {pool. addLast (Connection) proxyedConnection); pool. Y ();} return null;} // return value of the target method Object returnValue = method. invoke (con, args); return returnValue ;}}); pool. add (Connection) proxyedConn);} catch (Exception e) {throw new RuntimeException (e. getMessage (), e) ;}} public Connection getConnection () throws SQLException {synchronized (pool) {if (pool. size () = 0) {try {pool. wait ();} catch (InterruptedException e) {e. printStackTrace ();} return getConnection ();} Connection con = pool. removeFirst (); System. err. println ("siize:" + pool. size (); return con ;}}}

 

Related Article

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.