在get中调用update居然可以成功的修改,正常来说应该会死锁的呀?
- Java code
public class UserDao { public void get(String username){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); PreparedStatement pstat = conn.prepareStatement("select * from user2 where username=?"); pstat.setString(1,username); ResultSet rs = pstat.executeQuery(); if(rs.next()){ System.out.println(rs.getString(2)); } rs.close(); pstat.close(); update(username,"2dddddddd");//更新上面查询的字段 conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void insert(String username, String password){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); PreparedStatement pstat = conn.prepareStatement("insert into user2 values(?,?)"); pstat.setString(1, username); pstat.setString(2, password); pstat.execute(); pstat.close(); conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void update(String username, String password){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); PreparedStatement pstat = conn.prepareStatement("update user2 set password=? where username=?"); pstat.setString(2, username); pstat.setString(1, password); pstat.execute(); pstat.close(); conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void main(String[] args) { UserDao dao = new UserDao(); dao.get("u1"); }}
------解决方案--------------------------------------------------------
update(username,"2dddddddd");//更新上面查询的字段
怎么会死锁?你上面的是查询语句,除非查询的时候加FOR UPDATE(加排它锁X),否则不会影响更新