Java Study Notes-JDBC6 (21), study notes jdbc6
1. Use a dynamic proxy to write the connection pool
Design:
Proxy target: Native connection.
Proxy purpose: Modify the close method so that the close method cannot close the connection and actively detaches the connection.
Communicate with the thread through a dynamic Proxy:
1: proxy for Cxonnection.
2: The Connection is obtained through synchronization. If there is no Connection, the thread enters the waiting pool.
3: Modify the close method and wake up the waiting thread after the connection is still in progress.
Package cn. itcast. utils; 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. util. arrayList; import java. util. list; public class ConnUtils3 {// Step 1: declare the Connection pool to maintain all connections in the private static List <Connection> pool = new ArrayList <Connection> (); // Step 2: create multiple connections in the static code block static {try {Class. forName ("com. m Ysql. jdbc. Driver "); String url =" jdbc: mysql: // db909? CharacterEncoding = UTF8 "; for (int I = 0; I <3; I ++) {final Connection con = DriverManager. getConnection (url, "root", "1234"); // com. mysql. jdbc. jdbc4Connection @ // dynamic Proxy Object proxyedCon = Proxy for con Object. newProxyInstance (ConnUtils3.class. getClassLoader (), new Class [] {Connection. class}, // declare the execution handle, and only intercept the new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] args) throws Thro Wable {if (method. getName (). equals ("close") {System. err. println ("some people want to close the connection, cannot close, still connect"); // Add the proxy to the pool. This proxy is proxyedCon synchronized (pool) {pool. add (Connection) proxy); pool. required y ();} return null;} else {System. err. println ("allow" + method. getName (); return method. invoke (con, args) ;}}); // you must add the proxy object to the pool. Pool. add (Connection) proxyedCon);} catch (Exception e) {throw new RuntimeException (e. getMessage (), e) ;}}/*** provides a static factory method to return a Connection */public static Connection getCon () {synchronized (pool) {if (pool. size () = 0) {try {pool. wait ();} catch (InterruptedException e) {e. printStackTrace ();} return getCon ();} Connection con = pool. remove (0); // return a proxy connection object System. err. println ("more:" + pool. size (); return con ;}}}
Code optimization
Read a resource file through the Class Loader:
SomeClass. class. getReesource (xxx)-obtains xxx files in the same directory as the SomeCalss bytecode.
SomeClass. class. getClassLoader (). getResource ("xxxx");-get the xxx file under the classpath root.
1: Write url, driver, name, pwd to a configuration file. -Properties
2: maintain a pool through the inventory list.
Read resource files and obtain information
// Declare the resource class-Properties prop = new Properties (); // obtain the path URL of this file = ConnUtils3.class. getClassLoader (). getResource ("jdbc. properties "); String path = url. getPath (); // to prevent Chinese characters or spaces, path = URLDecoder. decode (path, "UTf-8"); File file = new File (path); // load jdbc. properties file prop. load (new FileInputStream (file); // obtain information String driver = prop. getProperty ("driver"); Class. forName (driver); String jdbcurl = pr Op. getProperty ("url"); String nm = prop. getProperty ("name"); String pwd = prop. getProperty ("pwd"); // create three native connections, both proxy String poolSize = prop. getProperty ("poolSize"); int size = Integer. parseInt (poolSize); for (int I = 0; I <size; I ++) {// create multiple connections and proxy each connection. Intercept the close method and connect again.
Function of Connection Pool
1: maintain multiple connections.
Create a List during initialization and put multiple connections to the List.
2: The connection can be recycled at close.
Dynamic proxy for Connection,
Intercept the close method.