backgroundIn Java program implementation, we often apply to the mechanism of transaction, transaction opening in the business layer, creating the database connection, invoking the DAO layer method for database access, the process needs to pass the database connection connection as the parameter to the DAO layer method. Obviously, such an implementation is not conducive to the reuse of the DAO layer method, when we do not use transactions, we need to create a database connection in the DAO layer method, so that DAO layer method without connection parameters can make the method more independent, clear, how to solve such an embarrassment? For this, we use the threadlocal to solve.
Basic Introductionthe "Local thread variable" can be understood as putting the variable into threadlocal, sharing it in the same thread, separating the resources between multiple threads, not interfering with each other, and ensuring thread safety.
Threadlocal<t> can be thought of as a map type, which can be understood to be stored and retrieved in the way of access key-value pairs, but it is necessary to make it clear that once a threadlocal is identified, it can only access a key-value pair, so its reading and fetching methods are relatively simple:
Get, set method--for reading, setting values in thread variables;
Remove Method--Removes the value of the current thread for this thread's local variables;
initialvalue--returns the current thread's initial value for this thread's local variable.
Package Connection
Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import java.sql.statement;/** * with threadlocal package connection * * @author Administrator * */public class ConnectionManager {/ /define ThreadLocal static variable, determine access type connectionprivate static threadlocal<connection> Connectionholder = new ThreadLocal <Connection> ();/** * Get Connection * @return */public static Connection getconnection () {Connection conn = Connection Holder.get ();//If the corresponding connectionif (conn = = null) is not bound in the current thread {try {class.forname ("oracle.jdbc.driver.OracleDriver"); String url = "Jdbc:oracle:thin: @localhost: 1521:bjpowern"; String username = "DRP1"; String password = "DRP1"; conn = drivermanager.getconnection (URL, username, password);// Set connection to Threadlocalconnectionholder.set (conn);} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ()}} Return conn;} /** * Close Database connection method * @return */public static void CloseConnection () {Connection COnn = Connectionholder.get (); if (conn! = null) {try {conn.close ();//Clear threadlocal from Connectionconnectionholder.remove ( );} catch (SQLException e) {e.printstacktrace ();}}} /** * Close Database connection method * @return */public static void Close (Connection conn) {if (conn! = null) {try {conn.close ();} catch (Sqlex Ception e) {e.printstacktrace ();}}} public static void Close (Statement pstmt) {if (pstmt! = null) {try {pstmt.close ();} catch (SQLException e) {e.printstacktr Ace ();}}} public static void Close (ResultSet rs) {if (rs! = null) {try {rs.close ();} catch (SQLException e) {e.printstacktrace ();} }}/** * Transaction Open * @return */public static void BeginTransaction (Connection conn) {try {if (conn! = null) {if (conn.getautocom MIT ()) {Conn.setautocommit (false);//Manual Commit}}}catch (SQLException e) {}}/** * Transaction commit * @return */public static void CommitTrans Action (Connection conn) {try {if (conn! = null) {if (!conn.getautocommit ()) {Conn.commit ();}}} catch (SQLException e) {}}/** * transaction rollback * @return */public static void RollbacktransactioN (Connection conn) {try {if (conn! = null) {if (!conn.getautocommit ()) {Conn.rollback ();}}} catch (SQLException e) {}}}
After the above encapsulation, the ConnectionManager class controls the creation and acquisition of the line range connection, so that the DAO layer method does not need to pass connection parameters to realize the effect of the business logic Layer control transaction. Threadlocal can also be applied in a number of other cases, which are explained later in the use.
Summary
Multithreading is a common technology to solve high concurrency, clear the resource sharing between threads, reasonable allocation and control of the coordination between resources, you can use a lot of threads, is also the premise of the application of multithreading, in this area is still very deficient, a lot of accumulated learning!
"Java Technology drip"--threadlocal encapsulating JDBC Transaction operations