Write Data Sources (not written during development) and use dynamic proxy to overwrite the clos of Connection

Source: Internet
Author: User
Importjava. io. printWriter; importjava. lang. reflect. invocationHandler; importjava. lang. reflect. method; importjava. lang. reflect. proxy; importjava. SQL. connection; importjava. SQL. SQLException; importjava. util. using list; importjavax. SQL. dataS

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. SQLException; import java. util. export list; import javax. SQL. dataS

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. SQLException; import java. util. export list; import javax. SQL. dataSource; import com. itheima. util. jdbcUtil; // standard database connection pool. Literally, it is called a data source (all with a pool, to improve efficiency) public class MyDataSource implements DataSource {private static tables list
 
  
Pool = new external list
  
   
(); Static {try {// pool initialization: 10 connections for (int I = 0; I <10; I ++) {pool. add (JdbcUtil. getConnection () ;}} catch (Exception e) {throw new ExceptionInInitializerError ("database connection pool initialization failed") ;}// return its packaging class, all methods in connection @ Overridepublic synchronized Connection getConnection () throws SQLException {if (pool. size ()> 0) {final Connection conn = pool. removeFirst (); // The connection Implementation of the original data. Encapsulated Connection proxyconn = (Connection) Proxy. newProxyInstance (conn. getClass (). getClassLoader (), conn. getClass (). getInterfaces (), new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. out. println (method. getName (); if ("close ". equals (method. getName () {return pool. add (conn);} else {return method. invoke (conn, args) ;}}); return proxyconn ;}elsethrow new RuntimeException ("busy server") ;}@ Overridepublic PrintWriter getLogWriter () throws SQLException {return null ;} @ Overridepublic void setLogWriter (timed out) throws SQLException {}@ Overridepublic void limit (int seconds) throws SQLException {}@ Overridepublic int limit () throws SQLException {return 0;} @ Overridepublic
   
    
T unwrap (Class
    
     
Iface) throws SQLException {return null;} @ Overridepublic boolean isWrapperFor (Class
     Iface) throws SQLException {return false;} @ Overridepublic Connection getConnection (String username, String password) throws SQLException {return null ;}}
    
   
  
 
Package com. jxnu. util; import java. io. inputStream; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. properties; // common tool class: public class JdbcUtil {private static String driverClass; private static String url; private static String username; private static String password; static {try {// load the configuration file In PutStream in = JdbcUtil. class. getClassLoader (). getResourceAsStream ("dbcfg. properties "); Properties props = new Properties (); props. load (in); driverClass = props. getProperty ("driverClass"); url = props. getProperty ("url"); username = props. getProperty ("username"); password = props. getProperty ("password"); Class. forName (driverClass);} catch (Exception e) {throw new ExceptionInInitializerError (e) ;}// obtain the database connection Public static Connection getConnection () throws Exception {Connection conn = DriverManager. getConnection (url, username, password); return conn;} // release the occupied resource public static void release (ResultSet rs, Statement stmt, Connection conn) {if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}


Test:

Package com. jxnu. pool04; import java. SQL. connection; import java. SQL. SQLException; import java. SQL. statement; import org. junit. test; public class Dao1Impl {MyDataSource ds = new MyDataSource (); @ Testpublic void add () {Connection conn = null; Statement stmt = null; try {conn = ds. getConnection (); stmt = conn. createStatement (); // com. mysql. jdbc. connection: MySQL // com. oracle. jdbc. connection: Oracle //... system. out . Println (conn. getClass (). getName ();} catch (Exception e) {e. printStackTrace () ;} finally {if (conn! = Null) {try {conn. close (); // closes the connection. Should not be switched off. Put it back into the pool. How can I put it back into the pool? Connection Pool highlights} catch (SQLException 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.