Java Web-based JdbcUtils tool class final and javawebjdbcutils

Source: Internet
Author: User

Java Web-based JdbcUtils tool class final and javawebjdbcutils

The final version of JdbcUtils tool class 3.0 adds transaction-related functions and release links. The final version can be directly packaged into a jar package, which will be used in subsequent basic projects.

1. JdbcUtils code

1/** 2 * Final Version 3 * @ author hui. zhang 4*5 */6 public class JdbcUtils {7 // default configuration file, must be given c3p0-config.xml 8 private static combooleddatasource dataSource = new combooleddatasource (); 9 10 // transaction dedicated Connection 11 private static ThreadLocal <Connection> tl = new ThreadLocal <Connection> (); 12/** 13 * use the Connection pool to return a Connection object 14 * @ return15 * @ throws SQLException16 */17 public static Connection getConnection () throws SQLExc Eption {18 Connection con = tl. get (); 19 // when con ratio is null, transaction 20 if (con! = Null) return con; 21 return dataSource. getConnection (); 22} 23 24/** 25 * returned connection pool object 26 * @ return27 */28 public static DataSource getDataSource () {29 return dataSource; 30} 31 32/** 33 * Start transaction 34*1. get a Connection and set its setAutoCommit (false) 35*2. make sure that the Connection used in dao is the 36 * @ throws SQLException 37 */38 public static void beginTransaction () throws SQLException {39 Connection con = tl. get (); 40 if (con! = Null) throw new SQLException ("the transaction has been enabled. Please do not enable it again! "); 41 con = getConnection (); 42 con. setAutoCommit (false); 43 tl. set (con); 44} 45 46/** 47 * commit transaction 48*1. obtain the Connection provided by beginTransaction, and then call the commit method 49 * @ throws SQLException 50 */51 public static void commitTransaction () throws SQLException {52 Connection con = tl. get (); 53 if (con = null) throw new SQLException ("the transaction has not been started and cannot be committed! "); 54 con. commit (); 55 con. close (); 56 tl. remove (); 57} 58 59/** 60 * roll back transaction 61*1. obtain the Connection provided by beginTransaction, and then call the rollback method 62 * @ throws SQLException 63 */64 public static void rollbackTransaction () throws SQLException {65 Connection con = tl. get (); 66 if (con = null) throw new SQLException ("the transaction has not been started and cannot be rolled back! "); 67 con. rollback (); 68 con. close (); 69 tl. remove (); 70} 71 72/** 73 * release Connection 74 * @ param connection75 * @ throws SQLException 76 */77 public static void releaseConnection (connection Connection) throws SQLException {78 Connection con = tl. get (); 79 // determines whether it is a dedicated transaction connection. if it is not necessary to close 80 if (con = null) 81 connection. close (); 82 // If con! = Null indicates that a transaction exists. You need to determine whether the parameter connection is the same as con! = Connection) 85 connection. close (); 86} 87}

2. Give the c3p0-config.xml configuration file under src

<? Xml version = "1.0" encoding = "UTF-8"?> <C3p0-config> <! -- Default configuration information --> <default-config> <! -- Four major connection parameters --> <property name = "user"> root </property> <property name = "password"> 123 </property> <property name = "driverClass"> com. mysql. jdbc. driver </property> <property name = "jdbcUrl"> jdbc: mysql: // mydb </property> <! -- Pool parameter configuration --> <property name = "acquireIncrement"> 3 </property> <property name = "initialPoolSize"> 10 </property> <property name = "minPoolSize"> 2 </property> <property name = "maxPoolSize"> 10 </property> </default-config> </c3p0-config>

3. Summary

From the first basic version 1.0 to the connection pool 2.0 to the current transaction, one step at a time. Every version should be left... Let us know new things !!!

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.