多线程_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
- 多线程_10_ThreadLocal案例之JDBC
- 多线程学习之死锁案例
- JDBC案例之基础查询mysql
- jdbc案例
- Java多线程之读写锁经典案例
- Java多线程之线程分类【案例分析】
- 多线程小案例之龟兔赛跑
- 多线程基础部分之生产消费者案例
- java多线程之生产者与消费者案例
- java多线程之生产者和消费者案例
- Java多线程之线程状态案例
- 多线程案例
- 多线程案例
- 多线程案例
- 多线程案例
- 多线程案例
- 多线程之futureTask(future,callable)实例,jdbc数据多线程查询
- jdbc的简单案例
- Java 图书馆库存管理系统(附加用户购买结账系统)
- 如何查看MySQL的版本?
- 【POJ2559】Largest Rectangle in a Histogram
- 简单的linux命令
- 交换机
- 多线程_10_ThreadLocal案例之JDBC
- ZooKeeper 客户端的使用
- libevent参考手册第二章:创建event_base
- Android sd卡中 storage/emulated/0目录下找不到所保存文件的问题
- hibernate和mybatis比较
- 机房收费系统之代码库
- easyui 屏蔽字符串
- HTTP协议简介
- HDU 5862 Counting Intersections(离散化+树状数组)