Jdbcutils tools that can do business

Source: Internet
Author: User

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

Related Article

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.