The following is a brief description of several core classes
Database Connection Controller
Public classConnectionManager {Private StaticThreadlocal<connection> Connectionholder =NewThreadlocal<connection>(); //gets a copy of the current local thread local variable---Get the connection Public StaticConnection getconnection () {Connection conn=Connectionholder.get (); if(conn = =NULL) {Conn=Newdbutil (). Getconn (); Connectionholder.set (conn); } returnConn; } /*** Close Database connection*/ Public Static voidCloseConnection () {Connection conn=Connectionholder.get (); if(Conn! =NULL) { Try{conn.close (); Connectionholder.remove (); } Catch(SQLException e) {e.printstacktrace (); } } } /*** Open Transaction manually * *@paramConn*/ Public Static voidBeginTransaction (Connection conn) {Try { if(Conn! =NULL) { if(Conn.getautocommit ()) {Conn.setautocommit (false); } } } Catch(SQLException e) {e.printstacktrace (); } } /*** SET TRANSACTION submission * *@paramConn*/ Public Static voidCommitTransaction (Connection conn) {Try { if(Conn! =NULL) { if(!Conn.getautocommit ()) {Conn.commit (); } } } Catch(Exception e) {e.printstacktrace (); } } /*** SET TRANSACTION rollback * *@paramConn*/ Public Static voidRollbackTransaction (Connection conn) {Try { if(Conn! =NULL) { if(!Conn.getautocommit ()) {Conn.rollback (); } } } Catch(Exception e) {e.printstacktrace (); } } /*** Restore the original transaction commit * *@paramConn*/ Public Static voidrecovertransaction (Connection conn) {Try { if(Conn! =NULL) { if(Conn.getautocommit ()) {Conn.setautocommit (false); } Else{conn.setautocommit (true); } } } Catch(Exception e) {e.printstacktrace (); } }}
Public Abstract class Abstractdao { protectednull; protected NULL ; Public Abstractdao () { this. conn = connectionmanager.getconnection (); }}
Data Access Layer Factory: Daofactory
Public class daofactory { publicstaticthrows Exception { = Resourceutil.getresourcebundle ("Database", "Db.name"); = Resourceutil.getresourcebundle (baseName, "Member.class"); return (Imemberdao) class.forname (className). newinstance ();
Business layer proxy class: Serviceproxy
Public classServiceproxyImplementsInvocationhandler, Serializable {PrivateObject obj;//Real Themes Publicobject bind (Object obj) { This. obj = obj;//implementation of the Memberserviceimpl returnproxy.newproxyinstance (Obj.getclass (). getClassLoader (), obj. GetClass (). Getinterfaces (), This); } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object value=NULL; //Initialize validation of database connections when binding objects if(Method.getname (). Contains ("Login") || Method.getname (). Contains ("Add") || Method.getname (). Contains ("Edit") || Method.getname (). Contains ("rm")) {//If the update operation is nowConnection conn =connectionmanager.getconnection (); Try{connectionmanager.begintransaction (conn); Value= Method.invoke ( This. obj, args); Connectionmanager.committransaction (conn); } Catch(Exception e) {connectionmanager.rollbacktransaction (conn); } finally{connectionmanager.recovertransaction (conn); Connectionmanager.closeconnection (); } } Else { Try{Value= Method.invoke ( This. obj, args); }Catch(Exception e) {Throwe; } finally{connectionmanager.closeconnection (); } } returnvalue; }}
Business Layer Factory class:servicefactory
Public class servicefactory { publicstatic imemberservice getimemberserviceinstance () { returnnew serviceproxy () . bind (new Memberserviceimpl ());} }
MVC design Process (rough) practice