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

同时有两个线程同时访问这个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 block
			e.printStackTrace();
		}
	}

你可能感兴趣的:(C++,c,mysql,Hibernate,C#)