Answer the question
In order to leave comments, you need to log in
Organization of transaction processing in the Service Layer, does such an approach have the right to life?
Good afternoon to all residents! In the process of learning Java EE in courses, I encountered such a problem: it is
necessary to do transaction processing in the service layer in order to make the operation atomic for the backend user. Used by hibernate. spring for transactions is not yet allowed, and I'm looking at . I read, I'm looking for ways who implemented how and which implementation can be adequate .. there is one thought, but I want to know the advice, can someone tell me if it's right to do this.
Application - Administration of orders in a car service
I have 3 DAOs (they only contain crud, there are no transactions, work is being done through Session). There is a service, WorkShopService, which describes the logic of working with data.
Here, for example, is a code fragment from the service (I won’t give the whole one - it’s quite long)
public class WorkShopServiceImpl implements WorkShopService {
@ConfigProperty(propName = "orderDAO", type = OrderDAO.class)
private OrderDAO orderDAO;
// дао для рабочих-мастеров
@ConfigProperty(propName = "masterDAO", type = MasterDAO.class)
private MasterDAO masterDAO;
// дао для рабочий мест-гаражей (у меня может быть их несколько)
@ConfigProperty(propName = "workplaceDAO", type = WorkPlaceDAO.class)
private WorkPlaceDAO workplaceDAO;
// методы....
public List<WorkPlace> countFreeWorkPlace(Date date) {
// метод выводит список свободных рабочих мест на указанную дату
// исходя из количества мастеров, заказов
}
// методы....
}
public class TransactionalWorkShopService implements WorkShopService {
@ConfigProperty(propName = "service", type = WorkShopService.class)
private WorkShopService service;
@ConfigProperty(propName = "sessionProvider", type = SessionProvider.class)
private SessionProvider sessionProvider;
private Transaction tx;
public TransactionalWorkShopService() {
}
// какие то методы ...
//...
public List<WorkPlace> freeWorkPlace(Date date) {
try {
tx = sessionProvider.getSession().startTransaction();
List<WorkPlace> res = service.freeWorkPlace(date);
tx.commit();
return res;
} catch(HibernateException e) {
handleException(e);
throw e;
}
}
// какие то методы ...
//...
private void handleException(Exception e) {
try {
if(tx != null) tx.rollback();
} catch(Exception e1) {
log.error("Cannot rollback transaction",e1);
}
log.error(e);
}
}
Answer the question
In order to leave comments, you need to log in
Of course not. You will have to copy-paste these decorators for each service you want to wrap in a transaction. You can do something like this:
public interface TransactionalOperation {
void doOperation();
}
public class TransactionalHandler {
@Autowired
private SessionProvider sessionProvider;
void doTransaction(TransactionalOperation transactionalOperation) {
// Create transaction
transactionalOperation.doOperation();
// Commit and roolback handler
}
}
public class SomeService {
@Autowired
private TransactionalHandler transactionalHandler;
public void doMake() {
transactionalHandler.doTransaction(() -> {
// Do something in transaction
});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question