Jdbcutils Packaging for Tool classes
Package Cn.wht.utils;import Java.sql.connection;import Java.sql.sqlexception;import javax.sql.datasource;import Com.mchange.v2.c3p0.combopooleddatasource;public class Jdbcutils {private static DataSource datasource=null; Data source private static final threadlocal<connection> threadlocal=new threadlocal<connection> (); The thread container that binds the connection static{datasource=new combopooleddatasource ();} /** * Get Connection Pool * @return */public static DataSource Getdatasource () {return DataSource;} /** * Get connections * A connection to the @return database */public static Connection getconnection () {try {Connection connection=threadlocal.get (); if ( Connection==null) {connection=datasource.getconnection (); Threadlocal.set (connection); return connection;} Else{return Connection;}} catch (SQLException e) {throw new RuntimeException (e);}} /** * Close connection */public static void Close () {Connection connection=threadlocal.get (); if (connection!=null) {try { Connection.close ();} catch (SQLException e) {throw new RuntimeException (e);} Finally{threAdlocal.remove ();}}} /** * * Open transaction */public static void StartTransaction () {try {Connection connection=getconnection (); Threadlocal.set ( connection); Connection.setautocommit (true);} catch (SQLException e) {throw new RuntimeException (e);}} /** * COMMIT TRANSACTION */public static void commit () {try {Connection connection=threadlocal.get (); if (connection!=null) { Connection.commit ();}} catch (SQLException e) {throw new RuntimeException (e);}} /** * ROLLBACK TRANSACTION */public static void rollback () {try {Connection connection=threadlocal.get (); if (connection!=null) { Connection.rollback ();}} catch (SQLException e) {throw new RuntimeException (e);}}}
This filter does the business
Package Cn.wht.filter;import Java.io.ioexception;import Javax.servlet.filter;import javax.servlet.FilterChain; Import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import javax.servlet.ServletRequest; Import Javax.servlet.servletresponse;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Cn.wht.utils.jdbcutils;public class Opentransactioninviewfilter Implements Filter {@Overridepublic void init (Filterconfig filterconfig) throws Servletexception {//TODO auto-generated m Ethod stub} @Overridepublic void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {//TODO auto-generated method stubrequest= (httpservletrequest) request;response= ( HttpServletResponse) response;try{jdbcutils.starttransaction (); Chain.dofilter (request, response); Jdbcutils.commit ();} catch (Exception e) {jdbcutils.rollback ();//Jump to error page Request.getrequestdispatcher ("/error.jsp"). Forward (request, response);} Finally{jdbcutils.close ();}} @Overridepublic void Destroy () {//TODO auto-generated method stub}}
Jdbcutils tools that can do business