多线程_10_ThreadLocal案例之JDBC

来源:互联网 发布:2017淘宝设备管理在哪 编辑:程序博客网 时间:2024/05/27 19:27

引用:

http://my.oschina.net/huangyong/blog/159725

http://www.iteye.com/topic/103804

/* ThreadLocal 它就是一个容器,用于存放线程的局部变量,我认为应该叫做 ThreadLocalVariable(线程局部变量)才对, 真不理解为什么当初 Sun 公司的工程师这样命名。 早在 JDK 1.2 的时代,java.lang.ThreadLocal 就诞生了, 它是为了解决多线程并发问题而设计的,只不过设计得有些难用,所以至今没有得到广泛使用 可以解决同步抢占资源的问题 ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了线程保持对象的方法和避免参数传递的方便的对象访问方式  ThreadLocal的应用场合,最适合的是按线程多实例(每个线程对应一个实例)的对象的访问,并且这个对象很多地方都要用到。  */public class DBUtil {// 数据库配置private static final String driver = "com.mysql.jdbc.Driver";private static final String url = "jdbc:mysql://localhost:3306/test";private static final String username = "root";private static final String password = "root";// 定义一个用于放置数据库连接的局部线程变量(使每个线程都拥有自己的连接)private static ThreadLocal<Connection> connContainer = new ThreadLocal<Connection>();// 获取连接public static Connection getConnection() {Connection conn = connContainer.get();try {if (conn == null) {Class.forName(driver);conn = DriverManager.getConnection(url, username, password);}} catch (Exception e) {e.printStackTrace();} finally {connContainer.set(conn);}return conn;}// 关闭连接public static void closeConnection() {Connection conn = connContainer.get();try {if (conn != null) {conn.close();}} catch (Exception e) {e.printStackTrace();} finally {connContainer.remove();}}}


public interface PersonService {  void updatePerson(long personId, String personName);}


public class PersonServiceImpl implements PersonService{private static final String UPDATE_PERSON_SQL = "update person set name = ? where id = ?";private static final String INSERT_LOG_SQL = "insert into person_log (created, description) values (?, ?)";@Overridepublic void updatePerson(long personId, String personName) {try {// 获取连接Connection conn = DBUtil.getConnection();conn.setAutoCommit(false); // 关闭自动提交事务(开启事务)// 执行操作updatePerson(conn,  UPDATE_PERSON_SQL,  personName, personId); // 更新PERSONinsertLog(conn, INSERT_LOG_SQL, "Create product."); // 插入日志// 提交事务conn.commit();} catch (Exception e) {e.printStackTrace();} finally {// 关闭连接DBUtil.closeConnection();}}private void updatePerson(Connection conn, String updateProductSQL, String personName,long personId )throws Exception {PreparedStatement pstmt = conn.prepareStatement(updateProductSQL);pstmt.setString(1, personName);pstmt.setLong(2, personId);int rows = pstmt.executeUpdate();if (rows != 0) {System.out.println("Update product success!");}}private void insertLog(Connection conn, String insertLogSQL,String logDescription) throws Exception {PreparedStatement pstmt = conn.prepareStatement(insertLogSQL);pstmt.setString(1, UUID.randomUUID().toString());pstmt.setString(2, logDescription);int rows = pstmt.executeUpdate();if (rows != 0) {System.out.println("Insert log success!");}}public static void main(String[] args) { for (int i = 0; i < 140; i++) {    PersonService personService = (PersonService) new PersonServiceImpl();        ClientThread thread = new ClientThread(personService);        thread.start();    } /**    PersonService personService = (PersonService) new PersonServiceImpl();    personService.updatePerson(1, "test");*/}@Testpublic  void threadTest(String[] args) {   }}


1 0
原创粉丝点击