I have Dao Service that make order and If I don't found a car I don't need to commit
@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;
}
I have methods in DaoFactory that working with connection(open connection, start transaction,close connection,close transaction and make rollback)
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);
}
}
In my example If i dont want to commit my transaction what should I do?Call rollback?Or call commit and after that rollback? Also, if you could tell me how to catch exceptions in these methods, or let the method throw them and catch them directly in the service layer, because I don't quite understand how it is done. Thanks for reply.
If you don't want to commit a transaction you need use ROLLBACK
.
If you COMMIT
the transaction there is nothing left to ROLLBACK
, these operations are mutually exclusive.
These operations that happens should be catch wherever are the logic of your application. Some times after a exception happen you need to do more things than a ROLLBACK
. If you capture the exception in your DAO class you can't tell exactly what was happening and generate better messages or specific logic.