Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果我启动了事务,但由于条件的原因,我不应该';如果不这样做,我应该使用commit还是可以立即调用rollback?_Java_Mysql_Jdbc_Commit_Rollback - Fatal编程技术网

Java 如果我启动了事务,但由于条件的原因,我不应该';如果不这样做,我应该使用commit还是可以立即调用rollback?

Java 如果我启动了事务,但由于条件的原因,我不应该';如果不这样做,我应该使用commit还是可以立即调用rollback?,java,mysql,jdbc,commit,rollback,Java,Mysql,Jdbc,Commit,Rollback,我有Dao服务可以下订单,如果我没有找到车,我不需要承诺 @Override public String makeOrder(String[] stingNumbers, String[] categories, String userAddress, String userDestination, String login) { int[] numbers = Stream.of(stingNumbers).mapToInt(Integer::parseInt).to

我有Dao服务可以下订单,如果我没有找到车,我不需要承诺

 @Override
    public String makeOrder(String[] stingNumbers, String[] categories, String userAddress, String userDestination, String login) {
        int[] numbers = Stream.of(stingNumbers).mapToInt(Integer::parseInt).toArray();
        Car[] foundCars = new Car[categories.length];
        String messageTakenTime = null;
        MySQLDAOFactory.createConnectionScope();
        MySQLDAOFactory.createTransaction();
        User foundUser = userDao.findUser(login);
        for (int i = 0; i < foundCars.length; i++) {
            foundCars[i] = carDao.findCar(numbers[i], categories[i]);
            if (foundCars[i] == null) {
                MySQLDAOFactory.endTransaction();
                MySQLDAOFactory.abortTransaction();
                MySQLDAOFactory.endConnectionScope();
                return String.format("false %s %d", categories[i], numbers[i]);
            }
            carDao.updateCar(foundCars[i].getCarId(), "on Order");
            double distance = DistanceUtil.getDistance(userAddress, userDestination);
            CarCategory foundCarCategory = categoryDao.findCarCategory(categories[i]);
            double discount = foundCarCategory.getDiscount();
            double costPerKilo = foundCarCategory.getCostPerOneKilometer();
            int scale = (int) Math.pow(10, 1);
            double orderCost = (double) Math.round((distance * costPerKilo) - ((distance * costPerKilo) * discount) * scale) / scale;
            Order order = new Order();
            order.setUserId(foundUser.getUserId());
            order.setCarId(foundCars[i].getCarId());
            order.setOrderDate(LocalDateTime.now());
            order.setUserAddress(userAddress);
            order.setUserDestination(userDestination);
            order.setOrderCost(orderCost);
            orderDao.insertOrder(order);
            if (messageTakenTime == null) {
                messageTakenTime = DistanceUtil.takenTime(distance);
            }
        }
        MySQLDAOFactory.endTransaction();
        MySQLDAOFactory.endConnectionScope();
        return messageTakenTime;
    }

在我的示例中,如果我不想提交我的事务,我应该怎么做?调用rollback?或者在回滚之后调用commit?另外,如果您能告诉我如何在这些方法中捕获异常,或者让该方法抛出异常并直接在服务层中捕获异常,因为我不太了解它是如何完成的。感谢您的回复。

如果您不想提交事务,则需要使用
回滚

如果您
提交
事务,则没有任何内容可供
回滚
,这些操作是互斥的


无论应用程序的逻辑在哪里,都应该捕获发生的这些操作。有些时候,在异常发生后,您需要做的事情比回滚要多。如果您在DAO类中捕获了异常,您就无法准确地知道发生了什么,也无法生成更好的消息或特定逻辑。

谢谢您的回复。你能告诉我如何做到线程安全吗?这是一个技巧性的问题。事务绑定到连接,只要您使用相同的连接,您就处于相同的事务中。如果该连接结束,所有内容都将回滚。请注意使用池的连接对象。但是,除此之外,我不知道如何指导您。仅供参考:
connection.setAutoCommit(false)
不创建事务,它只是禁用自动提交模式。
public static void createTransaction() {
        isTransaction = true;
        try {
            connection.setAutoCommit(false);
        } catch (SQLException throwables) {
            LOGGER.error(throwables);
        }
    }

    public static void endTransaction() {
        try {
            connection.commit();
        } catch (SQLException throwables) {
            LOGGER.error(throwables);
        }
    }

    public static void abortTransaction() {
        try {
            connection.rollback();
        } catch (SQLException throwables) {
            LOGGER.error(throwables);
        }
    }

    public static void createConnectionScope() {
        isConnectionScope = true;
        try {
            connection = DATA_SOURCE.getConnection();
        } catch (SQLException e) {
            LOGGER.error(e);
        }
    }

    public static void endConnectionScope() {
        isConnectionScope = false;
        try {
            connection.close();
        } catch (SQLException throwables) {
            LOGGER.error(throwables);
        }
    }