六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 49|回复: 0

哈哈,终于被我搞成死锁了!

[复制链接]

升级  37.33%

28

主题

28

主题

28

主题

秀才

Rank: 2

积分
106
 楼主| 发表于 2013-1-27 05:40:33 | 显示全部楼层 |阅读模式
同时有两个线程同时访问这个servlet会导致一些问题:
第一个线程等待第二个的notify
而因为事物级别的提升导致第二个线程等待第一个线程的结束。
最终导致:死锁

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {try {Class.forName("com.mysql.jdbc.Driver");Connection c = DriverManager.getConnection("jdbc:mysql://localhost/hibernate", "root", "19841230");c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);c.setAutoCommit(false);Statement stmt = c.createStatement();ResultSet rs = stmt.executeQuery("select student.STUDENT_NAME " +"from student where student.STUDENT_NAME='FrenchMan' "); if(!rs.next()){synchronized(lock){if(i++%2==0){lock.wait();} else {lock.notify();}stmt.execute("insert into student(student_name) values('FrenchMan')");}}c.commit();rs.close();stmt.close();c.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表