** * Statement's self-encapsulation, blocking getResultSet, executeQuery, and getGeneratedKeys method Returns your own takeover * destination SQL action and setting the corresponding connection last activity time.
* @Author liudong * / package com.drsl.db; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; import java.lang.reflect. *; import sun.jdbc.odbc *;. public class StatementObject implements InvocationHandler {private Statement stm = null; private Statement stm_proxy = null; private final static String GETRESULTSET_METHOD_NAME = "getResultSet"; private final static String EXECUTEQUERY_METHOD_NAME = "executeQuery"; private final static String GETGENERATEDKEYS_METHOD_NAME = "getGeneratedKeys"; private ResultSetObject rso = null; public StatementObject (Statement stm) {this.stm = stm;} public Statement getStatement () {if (stm_proxy == null) {ClassLoader classloader = stm.getClass (). GetClassLoader (); class [] interfaces = stm.getclass (). getInterfaces (); if (interface == null || interfacs.length == 0) {interfaces = new class [1]; interfaces [0] = Statement. Clas s;} try {stm_proxy = (Statement) Proxy.newProxyInstance (classloader, interfaces, this);} catch (NullPointerException e) {log (e, "StatementObject getStatement () - error");} if (stm_proxy = null! ) log ( "StatementObject getStatement () - success");} return stm_proxy;} public Object invoke (Object proxy, Method m, Object [] args) throws Throwable {Object obj = null; log ( "StatementObject - invoke: Method: /"" m.getname() "
/ ""); // determine whether to invoke the getResultSet or executeQuery or getGeneratedKeys // it is intercepted if (GETRESULTSET_METHOD_NAME.equals (m.getName ()) || EXECUTEQUERY_METHOD_NAME.equals (m.getName ()) || GETGENERATEDKEYS_METHOD_NAME.equals (M.GetName ())) {ResultSet RS = (Resultset) M.Invoke (STM, ARGS); if (RS! = Null && RSO == Null) {r = new resultsTObject (rs); obj = rso.getResultset ();} Else if (rso! = Null) Obj = rso.get (); else log ("StatementObject - invoke: Method: /"" m.getname () "/"-- failed"); } else {obj = m.invoke (STM, ARGS);} // Set the last access time to clear the timeout connection setlastaccesstime (new date (). getTime ()); returnobj;} / ** * Text Information Writing Log File * / Private Void Log (String MSG) {ConnectionManager.log (MSG);} / ** * Writing Text Information and Abnormal Writing Log File * / Private Vo id log (Throwable e, String msg) {ConnectionManager.log (e, msg);} // set last visited private void setLastAccessTime (long ltime) {ConnectionObject.setLastAccessTime (ltime);}} // ** * PreparedStatement Self-encapsulation, blocking the ExecuteQuery method Returns your own takeover * destination SQL action and setting the final activity time.
* @Author liudong * / package com.drsl.db; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; import java.lang.reflect. *; public class PreparedStatementObject implements InvocationHandler {private PreparedStatement ps = null; private PreparedStatement ps_proxy = null; private final static String EXECUTEQUERY_METHOD_NAME = "executeQuery"; private ResultSetObject rso = null; PreparedStatementObject (PreparedStatement ps) {this.ps = ps;} public PreparedStatement getPreparedStatement () {If (ps_proxy == null) {ClassLoader ClassLoader = ps.getClass (). GetClassLoader (); class [] interfaces = ps.getClass (). GetInterfaces (); if (interfacs == null || interfaces.length == 0) {Interfaces = new class [1]; interfaces [0] = preparedStatement.class;} try {ps_proxy = (preparedStatement) Proxy.NewProxyInstance (ClassLoader, Interfaces, THIS );} Catch (NullPointerException e) {log (e, "PreparedStatementObject getPreparedStatement () - error");} if (ps_proxy = null) log (! "PreparedStatementObject getPreparedStatement () - success");} return ps_proxy;} Public Object Invoke (Object Proxy, Method M, Object [] args) throws throwable {object obj = null; log ("preparedStatementObject - invoke: method: /"" m.getname() "/" ";
// Whether to call EXECUTEQUERY if it is the takeover IF (executeQuery_method_name.equals (m.getname ())) {ResultSet RS = (Resultset) M.Invoke (PS, Args); if (RS! = Null && == null {RSO = New ResultSetObject (RS); obj = rso.get ();} else if (rso! = Null) obj = rso.getResultSet (); else log ("preparedStateNObject - Invoke: Method: /" M.GetName () "/" - failed ");} else {obj = m.invoke (ps, args);} Obj = m.invoke (ps, args); // Set the last access time so that Timely clearance setlastaccesstime (new Date (). GetTime ()); returnobj;} / ** * Write text information to log files * / private void log (String msg) {ConnectionManager.log (msg); / ** * Write text information and exception write log file * / private void log (throwable e, string msg) {connectionManager.log (e, msg);} // set the last access time Private Void SetLastAccesstime (long ltime) {ConnectionObject.SetLastAccesstime (LTIME }}}}} / ** * Statement self-encapsulation, shielding the getStatement method Returns your own takeover * destination SQL action and setting the corresponding connection last activity time.
* @Author liudong * / package com.drsl.db; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; import java.lang.reflect. *; public class ResultSetObject implements InvocationHandler {private ResultSet rs = null; private ResultSet rs_proxy = null; private final static String GETSTATEMENT_METHOD_NAME = "getStatement"; private final static String GETMETADATA_METHOD_NAME = "getMetaData"; private StatementObject stmo = null; private ResultSetMetaDataObject rsdmo = null; public ResultSetObject (ResultSet rs) {this.rs = rs;} public ResultSet getResultSet () {if (rs_proxy == null) {ClassLoader classloader = rs.getClass () getClassLoader ();. Class [] interfaces = rs.getClass ( ) .GetInterfaces (); if (interfaces == null || interfacs.length == 0) {interfaces = new class [1]; interfaces [0] = resultset.class; rs_proxy = (ResultSet) Proxy.newProxyInstance (classloader, interfaces, this);} catch (NullPointerException e) {log (e, "ResultSetObject getResultSet () - error");} if (rs_proxy = null!) log ( "ResultSet getResultSet () - success ");} return rs_proxy;} public Object invoke (Object proxy, Method m, Object [] args) throws Throwable {Object obj = null; log (" ResultSetObject - invoke: Method: / "" M.GetName () "/"); // Do not perform GetStatement,
Returns a STATEMENT Trip Class IF (getStatement_Method_name.equals (m.getname ())) {statement stm = (statement) M.INVOKE (RS, ARGS); if (STM! = Null && stmo == null) {stmo = new StatementObject (STM); Obj = stmo.getStatement ();} else if (stmo! = Null) obj = stmo.getStatement (); Else Log ("ResultSetObject - invoke: method: /" m.getname () "/" - failed ");} else if (getMetadata_method_name.equals (m.getname ())) {ResultSetMetadata RSDM = (RSDMETMETADATA) M.INVOKE (RS, ARGS); if (RSDM! = Null && = = NULL) {RSDMO = New ResultSetMetadataObject (RSDM); Obj = rsdmo.getMetadata ();} else if (RSDMO! = null) Obj = rsdmo.getMetadata (); else log ("ResultSetObject - invoke: Method: /" " M.GetName () " / "- failed");} else obj = m.invoke (rs, args); // Set the last access time to clear the timeout connection in time SetLastAccesstime (New Date (); gettime ()); returnobj;} / ** * Write text information to log file * / private void log (string msg) {ConnectionManager.log (msg);} / ** * Text information and exception write log file * / private void log (throwable e, string msg) {ConnectionManager.log (e, msg);} // Set the last access time private void setlastaccesstime (long LTIME) {ConnectionObject.setLastAccesstime LTIME);}} / ** * ResultSetMetAdata's self-encapsulation, shields the ExecuteQuery method Returns its own takeover * destination SQL action and setting the corresponding connection last activity time.
* @Author liudong * / package com.drsl.db; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; import java.lang.reflect. *; public class ResultSetMetaDataObject implements InvocationHandler {private ResultSetMetaData rsmd = null; private ResultSetMetaData rsmd_proxy = null; // public ResultSetMetaDataObject (ResultSetMetaData rsmd) {this.rsmd = rsmd;} public ResultSetMetaData getMetaData () {if (rsmd_proxy == null) {ClassLoader classloader = rsmd.getClass (). getClassLoader (); class [] interfaces = rsmd.getClass (). getInterface (); if (interfaces == null || interfaces.length == 0) {interfaces.length == 0) {Interfaces = new class [1]; interfaces [0] = ResultSetMetaData.class;} try {rsmd_proxy = (ResultSetMetaData) Proxy.newProxyInstance (classloader, interfaces, this);} catch (NullPointerException e) {log (e, "ResultSet MetaDataObject getPreparedStatement () - error ");} if (rsmd_proxy = null) log (!" ResultSetMetaDataObject getPreparedStatement () - success ");} return rsmd_proxy;} public Object invoke (Object proxy, Method m, Object [] args "Throws throwable {Object obj = null; log (" ResultSetMetAdataObject - Invoke: Method: /"" m.getname() "/ ""); obj = m.invoke (rsmd, args); // Set the last One visit time in order to clear the timeout connection setLastAccesstime (new date (). GetTime ()); returnobj;
} / ** * Write text information to log files * / private void log (string msg) {connectionManager.log (msg);} / ** * Writing text information and exception write log file * / private void log (throwable e, String msg) {ConnectionManager.log (e, msg);} // set last visited private void setLastAccessTime (long ltime) {ConnectionObject.setLastAccessTime (ltime);}} / ** * CallableStatement from packaging, shielding The ExecuteQuery getGeneratedKeys getMetadata method returns your own takeover * purpose to record SQL's actions and set up the final activity time.
* @Author liudong * / package com.drsl.db; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; import java.lang.reflect. *; public class CallableStatementObject implements InvocationHandler {private CallableStatement cs = null; private CallableStatement cs_proxy = null; private final static String EXECUTEQUERY_METHOD_NAME = "executeQuery"; private final static String GETGENERATEDKEYS_METHOD_NAME = "getGeneratedKeys"; private final static String GETMETADATA_METHOD_NAME = "getMetaData"; private ResultSetObject rso = null; private ResultSetMetaDataObject rsmdo = null; CallableStatementObject (CallableStatement cs) {this.cs = cs;} public CallableStatement getCallableStatement () {if (cs_proxy == null) {ClassLoader classloader = cs.getClass () getClassLoader ().; Class [] interfaces = cs.getClass (). GetInterfaces (); if (interfaces == null || interfacs.length == 0) {Interface faces = new Class [1]; interfaces [0] = CallableStatement.class;} try {cs_proxy = (CallableStatement) Proxy.newProxyInstance (classloader, interfaces, this);} catch (NullPointerException e) {log (e, "CallableStatementObject getCallableStatement () - error ");} if (cs_proxy = null) log (!" CallableStatementObject getCallableStatement () - success ");} return cs_proxy;} public Object invoke (Object proxy, Method m, Object [] args) throws Throwable {Object Obj =
Null; log ("CallableStatementObject - INVOKE: METHOD: / "" m.getname () "/" "); // Whether to call the executeQuery || getGeneratedKeys If it is taken if it is (ExecuteQuery_method_name.equals (M.Getname () || getGeneratedKeys_Method_name.Equals (M.GetName ())) {ResultSet RS = (ResultSet) M.Invoke (CS, ARGS); if (RS! = Null && == null) {RSO = New ResultSetObject RS); OBJ = rso.get ();} else if (rso! = null) obj = rso.get ("CallableStatementObject - invoke: Method: /"" m.getname() " / "- failed");} else if (getMetadata_method_name.equals (m.getname ())) {ResultSetMetadata RSMD = (ResultSetMetadata) M.INVOKE (CS, ARGS); if (RSMD! = Null && == NULL ) {RSMDO = New ResultSetMetadataObject (RSMD); obj = rsmdo.getMetadata ();} else if (rMDo! = Null) Obj = rsmdo.getMetadata (); Else Log ("CallableStatementObject - Invoke: Method: /"" m.getname" "/"-- failed ");} else obj = m.invoke (cs, args); // Set the last access time To clear the timeout connection setlastaccesstime (new date (). GetTime ()); returnobj;} / ** * Write text information to log files * / private void log (String msg) {ConnectionManager.log (MSG) } / ** * Write text information and exception write log file * / private void log (throwable e, string msg) {ConnectionManager.log (E, MSG);
} // Set the last access time private void setlastaccesstime (long LTIME) {connectionObject.setlastaccesstime (LTIME);}} Copyright Notice
Is this article helpful? Vote:
Yes
No Vote:
3
0
Discussant: Zhouke Participation: 228 Experts: 30 Posted: 2004-6-8 12:47 PM Should look very good, it is best to provide a simple usage example, and can make a multi-digital library Connect, such as Orcale, MSSQL, etc. also support!
Discussant: SHGG Participation: 32 Experts: 100 Published: 2004-6-10 3:30 in the afternoon, there is still a document that has not been posted.
DB.Properties is as follows (this file must be in the same directory in the same directory) ################################################################################################################################################################################################################################################################################ ######################## d d = D: //site/ -drsl/log.txt# drive (if there Multiple colons ':' interval) drivers = com.mysql.jdbc.driver: Oracle.jdbc.driver.OrCledriver: Net.SourceForge.jtds.jdbc.driver # database information block (mysql) mysql.url = jdbc: mysql : // localhost: 3306 / testmysql.option = & useUnicode = true & characterEncoding = UTF-8 & autoReconnect = falsemysql.user = aaamysql.password = bbbmysql.maxconn = 0mysql.minconn = 10mysql.maxonlinetime = 30 # database information block (oracle) oracle.url = jdbc: oracle: thin: @localhost: 1521: ORCLmysql.option = mysql.user = aaamysql.password = bbbmysql.maxconn = 0mysql.minconn = 10mysql.maxonlinetime = 30 # database information block (ms sql) oracle.url = jdbc : JTDS: SQLServer: //127.0.0.1: 1433 / mastemysql.option = mysql.user = aaamysql.pansword = bbbmysql.maxconn = 0mysql.Minconn = 10Mysql.MaxOnlineTime = 30 # database information block (....) # END File then modify ConnectionObject.newconnection () for private connectionObject newconnection () {connectionObject connobj = null; try {log ("end one part piece / r / n"); LOG ("Connection Pool" Name "creates a new connection object"); string url = url option; log ("URL =" URL); connection conn = drivermanager.getConnection (URL, User, Password); connobj = New ConnectionObject (conn, false); FreeConnection (Connne); CountConn ;} catch (sqlexception e) {log (e, "Unable to create the following URL connection:" URL "for user ="
User "Password =" password); return null;} Return connobj;} At the same time, for the preparedStatement interface, some JDBC does not support (such as mysql5.0 mysql-jdbc 3.0 will result in exceptions, the program exits CPU usage 100%) Therefore, please read the corresponding database and its JDBC usage documentation when using this connection pool. Discussant: Zhouke Participation: 228 Experts: 30 Posted: 2004-6-10 5:44 That is really good, try again, it is MSSQL and Oracle, so there is a question!
Discussant: SHEGG Participation: 32 Experts: 100 Published: 2004-6-11 9:09 am. . . . Document DB.Properties should be as follows (this file must be in the same directory in the same directory) #################################################################################################################################################################################################################################################################### ######################################################################################### (If there are multiple colons ':' interval) drivers = com.mysql.jdbc.driver: Oracle.jdbc.driver.Orcledriver: Net.SourceForge.jtds.jdbc.driver # tSQL) mysql.url = jdbc: mysql: // localhost:? 3306 / test mysql.option = & useUnicode = true & characterEncoding = UTF-8 & autoReconnect = falsemysql.user = aaamysql.password = bbbmysql.maxconn = 0mysql.minconn = 10mysql.maxonlinetime = 30 # database information block ( Oracle.URL = JDBC: Oracle: Thin: @localhost: 1521: OrcloCalHost: 1521: OrcloSer = Aaaracle.Password = bbboracle.maxconn = 0ORACLE.MINCONN = 10Oracle.MaxOnlineTime = 30 # database information block (MS SQL) MSSQL.URL = JDBC: JTDS: SQLServer: //127.0.0.1: 1433 / mastemssql.option = mssql.user = aaamssql.parsword = bbbmsql.maxconn = 0msql.minn = 10 mssql.maxonlinetime = 30 # database information block (.. ..) # End file
Commentator: shegg involved in minutes: 32 Expert: 100 Time: 2004-6-11 3:55 PM Correction: mysql.option = & useUnicode = true & characterEncoding = UTF-8 & autoReconnect = false to mysql.option = useUnicode = true & characterEncoding = UTF- 8 & automonnect = false
Guest: SINBOY Published: 2004-6-11 6:13 pm, it is very long, but there is no patience. I want to provide the client to provide a method of setting up DRIVER, URL and other parameters, and provide a method of _ResultSet Query (String SQL), int Execute (String SQL) to the client. Just, don't care about any underlying operation of establishing a connection, manage the connection. This is best to comment: SHEGG Participation: 32 Expert points: 100 Published: 2004-6-11 10:16 PM The following two points are mainly considered when designing the connection pool. In addition to getConnection (), other object instances (such as Connection, ResultSet, PreparedState, Statement, ETC) can be used directly, mainly because I take over these classes. (Please see the example in our program) 2. About setting DRIVER, set in the configuration file.
Guest: KING Published: 2004-7-8 9:06 AM Try, there is a mistake, more than why? Source Not Found for $ proxy1.executeQuery (String) Line: NOT AVAILABLES UNAVAILABLE]