Java learning notes-implementation of standard connection pool (27), java learning notes
Javax. SQL. DataSource.
Java. SQL .*
DataSource
The interface is implemented by the driver supplier. There are three types of implementations:
- Basic implementation-generate a standard Connection object-only one Connection exists in a DataSource data source, which is not managed by the pool.
- Connection Pool implementation [W1]-generate a Connection object that automatically participates in the Connection pool.
- 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 ;}}}