java-DB Transaction class in combination with C3P0 package

Source: Internet
Author: User

Bind the Connection object to the current thread so that the link can be used in each method.

Datasourceutils.java

Package Com.itheima.utils;import Java.sql.connection;import Java.sql.resultset;import java.sql.SQLException;import Java.sql.statement;import Javax.sql.datasource;import Com.mchange.v2.c3p0.combopooleddatasource;public Class datasourceutils {private static Combopooleddatasource ds=new Combopooleddatasource ();p rivate static threadlocal< Connection> tl=new threadlocal<> ();/** * Get Data source * @return Connection pool */public static DataSource Getdatasource () {return DS ;} /** * Get connection from current thread * @return connection * @throws SQLException */public static Connection getconnection () throws Sqlexception{connec  tion conn = Tl.get (); if (conn==null) {///First get create a connection and current thread bindings conn=ds.getconnection (); Bind Tl.set (conn);} Return conn;} /** * FREE Resources * * @param conn * connection * @param ST * Statement Performer * @param RS * result set */public static VO  ID Closeresource (Connection conn, Statement St, ResultSet rs) {Closeresource (St, RS); Closeconn (conn);} public static void Closeresource (Statement St, ResultSet rs) {CloseresulTset (RS); closestatement (ST);} /** * Release Connection * * @param conn * connection */public static void Closeconn (Connection conn) {if (conn! = null) {try {conn. Close ();//and current thread unbind Tl.remove ();} catch (SQLException e) {e.printstacktrace ();} conn = null;}} /** * Release Statement performer * * @param St * statement performer */public static void Closestatement (Statement st) {if (st! = null) {try {s T.close ();} catch (SQLException e) {e.printstacktrace ();} st = NULL;}} /** * Release Result set * * @param RS * result set */public static void Closeresultset (ResultSet rs) {if (rs! = null) {try {rs.cl OSE ();} catch (SQLException e) {e.printstacktrace ();} rs = null;}} /** * Open transaction * @throws SQLException */public static void StartTransaction () throws sqlexception{//get connection//Open transaction getconnection () . Setautocommit (false);} /** * Transaction commit */public static void Commitandclose () {try {//get connection Connection conn = getconnection ();//COMMIT Transaction Conn.commit ();// Release resource Conn.close ();//unbind Tl.remove ();} catch (SQLException e) {e.printstacktrace ();}} /** * Transaction rollback */public static void rollbAckandclose () {try {//get connection Connection conn = getconnection ();//Transaction rollback conn.rollback ();//Release Resources conn.close ();// Unbind Tl.remove ();} catch (SQLException e) {e.printstacktrace ();}}}

java-DB Transaction class in combination with C3P0 package

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.