如果我开始事务但由于条件,我不应该这样做,我应该使用提交还是可以立即调用回滚?
If I started transaction but because of the condition, I shouldn't do this, should I use commit or can I call rollback right away?
我有下订单的 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;
}
我在 DaoFactory 中有处理连接的方法(打开连接、开始事务、关闭连接、关闭事务并进行回滚)
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);
}
}
在我的示例中,如果我不想提交我的事务,我应该怎么做?调用回滚?或者调用提交然后回滚?另外,如果你能告诉我如何在这些方法中捕获异常,或者让方法抛出它们并直接在服务层捕获它们,因为我不太明白它是如何完成的。谢谢回复。
如果您不想提交交易,您需要使用 ROLLBACK
。
如果你COMMIT
交易没有什么可以ROLLBACK
,这些操作是互斥的。
无论您的应用程序的逻辑在哪里,都应该捕获发生的这些操作。有时在异常发生后你需要做的事情比 ROLLBACK
还多。如果您在 DAO 中捕获异常 class,您将无法准确判断发生了什么并生成更好的消息或特定逻辑。
我有下订单的 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;
}
我在 DaoFactory 中有处理连接的方法(打开连接、开始事务、关闭连接、关闭事务并进行回滚)
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);
}
}
在我的示例中,如果我不想提交我的事务,我应该怎么做?调用回滚?或者调用提交然后回滚?另外,如果你能告诉我如何在这些方法中捕获异常,或者让方法抛出它们并直接在服务层捕获它们,因为我不太明白它是如何完成的。谢谢回复。
如果您不想提交交易,您需要使用 ROLLBACK
。
如果你COMMIT
交易没有什么可以ROLLBACK
,这些操作是互斥的。
无论您的应用程序的逻辑在哪里,都应该捕获发生的这些操作。有时在异常发生后你需要做的事情比 ROLLBACK
还多。如果您在 DAO 中捕获异常 class,您将无法准确判断发生了什么并生成更好的消息或特定逻辑。