[JavaEE] database connection pool and dynamic proxy, javaee database connection
Implement the javax. SQL. DataSource Interface
Implement the Connection getConnection () method
Define a static member attribute; the member list type is used as the connection pool. initialize five database connections in the static code block and add them to the connection pool. In the getConnection method, remove a connection from the connection pool when obtaining the connection.
Import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. statement; public class JDBCTest {public static void main (String [] args) throws Exception {// Class Using Reflection. forName ("com. mysql. jdbc. driver "); // get the database connection. When importing a package, note that you must import java. in SQL, the interface-Oriented Programming MyPool pool = new MyPool (); Connection conn = pool. getConnection (); // get the transmitter object Statement statement = conn. createStatement (); // obtain the result set object ResultSet resultSet=statement.exe cuteQuery ("select * from user"); // traverse while (resultSet. next () {String username = resultSet. getString ("username"); System. out. println (username);} // closes the resource resultSet. close (); statement. close (); pool. resetConn (conn );}}
My connection pool
Import java. io. printWriter; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. SQLFeatureNotSupportedException; import java. util. using list; import java. util. list; import java. util. logging. logger; import javax. SQL. dataSource;/*** handwritten Connection pool ** @ author taoshihan **/public class MyPool implements DataSource {// Connection pool public static List <Connection> pool = new Initialize list <Connection> (); // initialize static {try {Class. forName ("com. mysql. jdbc. driver "); for (int I = 0; I <5; I ++) {Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/java", "root", "root"); pool. add (conn) ;}} catch (Exception e) {}}/*** get Connection */@ Override public Connection getConnection () throws SQLException {// if the pool is not connected if (pool. size () = 0) {for (int I = 0; I <5; I ++) {Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/java", "root", "root"); pool. add (conn) ;}}// first-in-first-out Connection conn = pool. remove (0); System. out. println ("get a connection, remaining pool" + pool. size (); return conn;}/*** reset Connection */public void resetConn (Connection conn) {try {if (conn! = Null &&! Conn. isClosed () {pool. add (conn); System. out. println ("a connection is returned, and the pool is still available" + pool. size () ;}} catch (Exception e) {e. printStackTrace () ;}@ Override public Connection getConnection (String username, String password) throws SQLException {return null ;}@ Override public PrintWriter getLogWriter () throws SQLException {// TODO Auto-generated method stub return null;} @ Override public void setLogWriter (Prin TWriter out) throws SQLException {// TODO Auto-generated method stub} @ Override public void setLoginTimeout (int seconds) throws SQLException {// TODO Auto-generated method stub} @ Override public int getLoginTimeout () throws SQLException {// TODO Auto-generated method stub return 0 ;} @ Override public Logger getParentLogger () throws SQLFeatureNotSupportedException {// TODO Auto-generated meth Od stub return null;} @ Override public <T> T unwrap (Class <T> iface) throws SQLException {// TODO Auto-generated method stub return null ;} @ Override public boolean isWrapperFor (Class <?> Iface) throws SQLException {// TODO Auto-generated method stub return false ;}}
Transform Methods in a class using inheritance, decoration, and dynamic proxy
Disadvantages of inheritance: At this time, we have obtained the Connection object, so we cannot transform this object through inheritance.
Test Implementation of decoration:
Import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. statement; public class JDBCTest {public static void main (String [] args) throws Exception {// test the decoration mode Animal dog = new BigDog (new Dog (); dog. eat (); dog. sound () ;}}/*** test the decoration mode ** @ author taoshihan **/interface Animal {public void eat (); public void sound ();} class Dog implements Animal {@ Override public void eat () {System. out. println ("eat") ;}@ Override public void sound () {System. out. println ("Wang") ;}}// in this case, I want to modify the sound method class BigDog implements Animal {private Dog; public BigDog (dog Dog) {this. dog = dog;}/*** call the original method */@ Override public void eat () {dog. eat ();}/*** this method is decorated */@ Override public void sound () {System. out. println ("yelling ");}}
Dynamic Proxy:
// Test proxy mode final Dog dog = new Dog (); Animal Proxy = (Animal) proxy. newProxyInstance (Dog. class. getClassLoader (), Dog. class. getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if ("sound ". equals (method. getName () {System. out. println ("yell"); return null;} else {return method. invoke (dog, args) ;}}); proxy. eat (); proxy. sound ();