I have the problem that after I process some information in my program i need to write the changes to a second database or get new information from it. Hibernate works fine for all work on single databases and also two databases work fine if I use the services provided to read and write the objects. My problem is that some of the attributes of some objects which are now eager in fetching should be lazy. with that I have to do some work on the objects outside of the transmissions. Also for data consisency issues I actually want that the function which works on both databases opens both transmissions and assigns the right sessions to the methods of either service.
example:
@Transactional("TransactionManager1")
class Service1 {
....
}
@Transactional("TransactionManager2")
class Service2 {
....
}
class MetaService {
@Autowired
Service1 service1;
@Autowired
Service2 service2;
public void meta() {
Object s1 = service1.getSomething();
Object s2 = service2.getSomeOtherStuff();
for ( Object some_lazy_attribute : s1.lazy_attributes ) {
Object s2_attribute = s2.getSomeLazyAttributeByID(s1.id);
if (s2_attribute == null) {
service2.createFrom(s2, some_lazy_attribute);
}
}
... iterate though a lazy attribute from s2 and do something else ...
}
}
for this example I would like to have some sort of transaction control that a rollback happens over both databases when an exception occurrs, also that both sessions get opened in the beginning and close at the end, so that the lazy attributes can be filled on the fly.
A workaround I thought of at the moment would be to keep the writing to a minimum and do it only at the very end, so that at least the writing happens per service & database in a transactional manner, and use hibernate.initialize in a getFuntion of the service, e.g.:
service1.getSomethingEagery();
but this would be tedious and feels like the wrong way to solve this problem.
Aucun commentaire:
Enregistrer un commentaire