V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jimisun
V2EX  ›  问与答

Jdbc Connection 为什么无法执行?

  •  
  •   jimisun · 2022-05-25 12:52:01 +08:00 · 504 次点击
    这是一个创建于 695 天前的主题,其中的信息可能已经有所发展或是发生改变。

    测试的是一个 JDBC 事务中可能包含对多个 JDBC 事务的操作,当该事务中的任何一个方法抛出异常后所有的 JDBC 事务均回滚。本例中 update2 的 preparedStatement2.executeUpdate()却无法执行,为什么?

    public class JdbcDemo21 {
    
        public static void main(String[] args) {
    
    
            try {//加载 MySql 的驱动类
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.out.println("找不到驱动程序类 ,加载驱动失败!");
                e.printStackTrace();
            }
            String url = "jdbc:mysql://1.fdsf229.fdsf:307/test";
            String dbusername = "root";
            String dbpassword = "root";
            try {
                Connection con = DriverManager.getConnection(url, dbusername, dbpassword);
                Connection con2 = DriverManager.getConnection(url, dbusername, dbpassword);
                con.setAutoCommit(false);
                con2.setAutoCommit(false);
                
                Savepoint conSavepoint = con.setSavepoint();
    
                update1(con);
                update2(con2, con, conSavepoint);
    
                con2.commit();
                con.commit();
    
                con.close();
                con2.close();
            } catch (SQLException se) {
                System.out.println("数据库连接失败!");
                se.printStackTrace();
            }
    
    
        }
    
        private static void update1(Connection con) throws SQLException {
            String sql = "update user set count = count - 100 where username = 'zhangsan'";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            int i = preparedStatement.executeUpdate();
        }
    
        private static void update2(Connection con, Connection connection, Savepoint conSavepoint) throws SQLException {
            try {
                String sql2 = "update user set count = count + 100 where username = 'lisi'";
                PreparedStatement preparedStatement2 = con.prepareStatement(sql2);
                int i1 = preparedStatement2.executeUpdate();
                int errorTemp = 1 / 0;
            } catch (RuntimeException runtimeException) {
                runtimeException.printStackTrace();
                con.rollback();
                connection.rollback(conSavepoint);
            }
    
        }
    }
    
    
    2 条回复    2022-05-26 08:52:12 +08:00
    huntagain2008
        1
    huntagain2008  
       2022-05-25 13:49:46 +08:00
    小白以为是连接问题,话说 catch 到的 异常是什么?
    jimisun
        2
    jimisun  
    OP
       2022-05-26 08:52:12 +08:00
    @huntagain2008 子方法异常控制父方法进行回滚
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2835 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.