Use ThreadLocal to maintain Connection

Source: Internet
Author: User

When using the examination system, we need to transmit the connection to ensure that all methods use the same connection when using transactions. Here, we use ThreadLocal to maintain the Connection to avoid this situation. ThreadLocal is easy to understand and take for granted as a "local thread ". In fact, ThreadLocal is not a Thread, but a local variable of the Thread. It may be easier to understand to name it ThreadLocalVariable. This class provides thread-local variables. These variables are different from their common counterparts, because each thread accessing a variable (through its get or set method) has its own local variable, which is independent of the initialization copy of the variable. ThreadLocal instances are usually private static fields in the class. They want to associate the state with a thread (for example, user ID or transaction ID), that is, in the same thread, this resource can be shared. Use ThreadLocal to maintain the Connection instance [java] package com. jialin. drp. util; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; /*** use ThreadLocal to maintain Connection * @ author jialin **/public class ConnectionManage {// used to save connection private static ThreadLocal <Connection> connectionHolder = new ThreadLocal <Connection> (); /** * Get the Connection * @ return */public static Connection GetConnection () {Connection conn = connectionHolder. get (); if (conn = null) {try {JdbcConfig jdbcConfig = XmlConfigReader. getInstance (). getJdbcConfig (); Class. forName (jdbcConfig. getDriverName (); conn = DriverManager. getConnection (jdbcConfig. getUrl (), jdbcConfig. getUserName (), jdbcConfig. getPassword ();} catch (ClassNotFoundException e) {// TODO Auto-genera Ted catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} connectionHolder. set (conn) ;}return conn ;}// close the public static void closeConnection () {Connection conn = connectionHolder. get (); if (conn! = Null) {try {conn. close (); // clear Connection holdonholder from ThreadLocal. remove ();} catch (SQLException e) {e. printStackTrace () ;}}// close Statement (the object used to execute a static SQL Statement and return the result it generates .) Public static void close (Statement pstmt) {if (pstmt! = Null) {try {pstmt. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the result set public static void close (ResultSet rs) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {e. printStackTrace () ;}}// enable the public static void beginTransaction (Connection conn) {try {if (conn! = Null) {if (conn. getAutoCommit () {conn. setAutoCommit (false); // manually submit }}} catch (SQLException e) {}} // submit the transaction public static void commitTransaction (Connection conn) {try {if (conn! = Null) {if (! Conn. getAutoCommit () {conn. commit () ;}} catch (SQLException e) {}} // rollback transaction public static void rollbackTransaction (Connection conn) {try {if (conn! = Null) {if (! Conn. getAutoCommit () {conn. rollback () ;}} catch (SQLException e) {}} package com. jialin. drp. util; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement;/*** use ThreadLocal to maintain Connection * @ author jialin **/public class ConnectionManage {// used to save connectionprivate static ThreadLocal <Connection> connectionHolder = ne W ThreadLocal <Connection> ();/*** get Connection * @ return */public static Connection GetConnection () {Connection conn = connectionHolder. get (); if (conn = null) {try {JdbcConfig jdbcConfig = XmlConfigReader. getInstance (). getJdbcConfig (); Class. forName (jdbcConfig. getDriverName (); conn = DriverManager. getConnection (jdbcConfig. getUrl (), jdbcConfig. getUserName (), jdbcConfig. getPassword ();} catch (ClassNotFoundException e ){ // TODO Auto-generated catch blocke. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} connectionHolder. set (conn) ;}return conn ;}// close the public static void closeConnection () {Connection conn = connectionHolder. get (); if (conn! = Null) {try {conn. close (); // clear ConnectionconnectionHolder from ThreadLocal. remove ();} catch (SQLException e) {e. printStackTrace () ;}}// close Statement (the object used to execute a static SQL Statement and return the result it generates .) Public static void close (Statement pstmt) {if (pstmt! = Null) {try {pstmt. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the result set public static void close (ResultSet rs) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {e. printStackTrace () ;}}// enable the public static void beginTransaction (Connection conn) {try {if (conn! = Null) {if (conn. getAutoCommit () {conn. setAutoCommit (false); // manually submit }}} catch (SQLException e) {}} // submit the transaction public static void commitTransaction (Connection conn) {try {if (conn! = Null) {if (! Conn. getAutoCommit () {conn. commit () ;}} catch (SQLException e) {}} // rollback transaction public static void rollbackTransaction (Connection conn) {try {if (conn! = Null) {if (! Conn. getAutoCommit () {conn. rollback () ;}} catch (SQLException e ){}}}

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.