使用ThreadLocal来解决问题
生活随笔
收集整理的這篇文章主要介紹了
使用ThreadLocal来解决问题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題:在設(shè)置dao層時(shí),public interface AccountDao {public void accountOut(String accountOut, double money) throws Exception;public void accountIn(String accountIn, double money) throws Exception;}那么我們自己去實(shí)現(xiàn)這個(gè)接口時(shí),怎樣處理,同一個(gè)Connection對(duì)象問題?使用ThreadLocalThreadLocal可以理解成是一個(gè)Map集合Map<Thread,Object>set方法是向ThreadLocal中存儲(chǔ)數(shù)據(jù),那么當(dāng)前的key值就是當(dāng)前線程對(duì)象.get方法是從ThreadLocal中獲取數(shù)據(jù),它是根據(jù)當(dāng)前線程對(duì)象來獲取值。如果我們是在同一個(gè)線程中,只要在任意的一個(gè)位置存儲(chǔ)了數(shù)據(jù),在其它位置上,就可以獲取到這個(gè)數(shù)據(jù)。關(guān)于JdbcUtils中使用ThreadLocal1.聲明一個(gè)ThreadLocalprivate static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();2.在getConnection()方法中操作Connection con = tl.get(); 直接從ThreadLocal中獲取,第一次返回的是null.if (con == null) {// 2.獲取連接con = DriverManager.getConnection(URL, USERNAME, PASSWORD);tl.set(con); //將con裝入到ThreadLocal中。}
package cn.learn.utils;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;public class JdbcUtils {private static final String DRIVERCLASS;private static final String URL;private static final String USERNAME;private static final String PASSWORD;private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();static {DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass");URL = ResourceBundle.getBundle("jdbc").getString("url");USERNAME = ResourceBundle.getBundle("jdbc").getString("username");PASSWORD = ResourceBundle.getBundle("jdbc").getString("password");}static {try {// 將加載驅(qū)動(dòng)操作,放置在靜態(tài)代碼塊中.這樣就保證了只加載一次.Class.forName(DRIVERCLASS);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection() throws SQLException {Connection con = tl.get();// 從ThreadLocal中獲取Connection。第一次獲取得到的是null.if (con == null) {// 2.獲取連接con = DriverManager.getConnection(URL, USERNAME, PASSWORD);tl.set(con); // 將con裝入到ThreadLocal中。}// tl.remove(); //解除return con;}// 關(guān)閉操作public static void closeConnection(Connection con) throws SQLException {if (con != null) {con.close();}}public static void closeStatement(Statement st) throws SQLException {if (st != null) {st.close();}}public static void closeResultSet(ResultSet rs) throws SQLException {if (rs != null) {rs.close();}}
}
package cn.learn.service;import java.sql.Connection;
import java.sql.SQLException;import cn.learn.dao.AccountDaoImpl;
import cn.learn.exception.AccountException;
import cn.learn.utils.JdbcUtils;public class AccountService {// 匯款方法// accountIn 收款人// accountOut 匯款人// money 金額public void account(String accountIn, String accountOut, double money) throws AccountException{// 在這個(gè)方法中要調(diào)用AccountDao中兩個(gè)方法AccountDaoImpl dao = new AccountDaoImpl();Connection con = null;try {con = JdbcUtils.getConnection();// 開啟事務(wù)con.setAutoCommit(false);dao.accountOut( accountOut, money);dao.accountIn( accountIn, money);} catch (Exception e) {e.printStackTrace();// 出現(xiàn)問題,進(jìn)行事務(wù)回滾if (con != null) {try {con.rollback();} catch (SQLException e1) {e1.printStackTrace();}}throw new AccountException(e.getMessage());} finally {// 事務(wù)提交// 關(guān)閉conif (con != null) {try {con.commit();con.close();} catch (SQLException e) {e.printStackTrace();}}}}}
package cn.learn.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import cn.learn.exception.AccountException;
import cn.learn.utils.JdbcUtils;public class AccountDaoImpl implements AccountDao {// 從accountOut賬戶轉(zhuǎn)出moneypublic void accountOut(String accountOut, double money)throws SQLException, AccountException {String sql = "update account set money=money-? where name=?";Connection con=JdbcUtils.getConnection();PreparedStatement pst = con.prepareStatement(sql);pst.setDouble(1, money);pst.setString(2, accountOut);int row = pst.executeUpdate();if (row == 0) {throw new AccountException("轉(zhuǎn)出失敗");}pst.close();}// 向accountIn賬戶轉(zhuǎn)入moneypublic void accountIn( String accountIn, double money)throws SQLException, AccountException {String sql = "update account set money=money+? where name=?";Connection con=JdbcUtils.getConnection();PreparedStatement pst = con.prepareStatement(sql);pst.setDouble(1, money);pst.setString(2, accountIn);int row = pst.executeUpdate();if (row == 0) {throw new AccountException("轉(zhuǎn)入失敗");}pst.close();}}
?
總結(jié)
以上是生活随笔為你收集整理的使用ThreadLocal来解决问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyDataSourceUtils使用T
- 下一篇: 解决2次查询User的问题(Thread