Create a connection pool using a proxy proxypool

Source: Internet
Author: User

Configuration file Properties

1 url=jdbc:mysql://127.0.0.1:3306/mine?characterencoding=utf-8

2 user=root

3 password=1234

4 driverclass=com.mysql.jdbc.driver

Main code

1  Packagejdbcutils;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.lang.reflect.InvocationHandler;6 ImportJava.lang.reflect.Method;7 ImportJava.lang.reflect.Proxy;8 Importjava.sql.Connection;9 ImportJava.sql.DriverManager;Ten Importjava.util.LinkedList; One Importjava.util.Properties; A /** - * Create a connection pool with a proxy -  * @authorASUS the  * -  */ -  Public classProxyconnutils { -  +     Private StaticLinkedlist<connection> pool =NewLinkedlist<>(); -     Private StaticString URL; +     Private StaticString user; A     Private StaticString password; at     Private StaticString Driverclass; -     //private static Connection conn; -     Static{ -         Try { -Properties Properties =NewProperties (); -InputStream in = Proxyconnutils.class. getResourceAsStream ("/db.properties"); in properties.load (in); -url = properties.getproperty ("url"); to System.err.println (URL); +user = Properties.getproperty ("User"); -Password = properties.getproperty ("Password"); theDriverclass = Properties.getproperty ("Driverclass"); * Class.forName (driverclass); $              for(inti = 0;i<3;i++){Panax Notoginseng                 FinalConnection conn =drivermanager.getconnection (URL, user, password); -                 //acting for connection. theObject connproxy = proxy.newproxyinstance (proxyconnutils.class. getClassLoader (), +                     NewClass[]{connection.class}, A                     NewInvocationhandler () { the                          + @Override -                          PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { $                             //determine if the Close method recycles the connection $                             if(Method.getname (). Equals ("Close")){ -                                 synchronized(pool) { -SYSTEM.ERR.PRINTLN ("Cannot close"); the Pool.addlast ((Connection) proxy); - pool.notify ();Wuyi                                     return NULL; the                                 } -}Else{ Wu                                 //If the call is not directly released by the Close method -                                 returnMethod.invoke (conn, args); About                             } $                         } -                     }); -                 //To add a proxy object to the pool - Pool.add ((Connection) connproxy); A             } +}Catch(Exception e) { the e.printstacktrace (); -             Throw NewRuntimeException (e); $         }  the     } the      the     //Get Connection Connection the      Public StaticConnection getconnection () { -         synchronized(pool) { in             if(pool.size () = = 0){ the                 Try { the pool.wait (); About}Catch(Exception e) { the e.printstacktrace (); the                     Throw NewRuntimeException (e); the                 } +             } -Connection Connection =Pool.removefirst (); the             returnconnection;Bayi         } the     } the}

Test code

1  Packagejdbctest;2 Importjava.sql.Connection;3 Importjava.sql.Statement;4 ImportJava.util.Scanner;5 ImportJdbcutils.connutil;6 Importjdbcutils.connutils;7 Importjdbcutils.proxyconnutils;8  Public classdemo01_tx3 {9     classOneextendsthread{Ten @Override One          Public voidrun () { ASystem.err.println ("1: Get Connection"); -Connection con = - proxyconnutils.getconnection (); the             Try{ -System.err.println ("2: Open Transaction"); -Con.setautocommit (false); -System.err.println ("3: Write Jack"); +Statement st =con.createstatement (); -St.execute ("INSERT into Money (Id,name) VALUES (1, ' Jack ')"); +System.err.println ("4: Submit ..."); A con.commit (); at}Catch(Exception e) { - e.printstacktrace (); -}finally { -                 Try { -System.err.println ("5: Close Connection"); -Con.setautocommit (true); in con.close (); -}Catch(Exception e) { to e.printstacktrace (); +                 } -             } the         }; *     }; $     Panax Notoginseng      Publicdemo01_tx3 () { -          for(inti=0;i<5;i++){ the             NewOne (). Start (); +         } A     } the  +      Public Static voidMain (string[] args) { -         Newdemo01_tx3 (); $     } $}

Run results

Create a connection pool using a proxy proxypool

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.