Spring application level transaction - spring

We are using a service provide which populates some general services. Some of the services need to be done in a transaction, like below:
service.transferAccount()
service.changeAccountType()
service.closeAccount()
So we need to develop a method TransderAccount_ChangeAccount_CloseAccount() which may look something like below
TransderAccount_ChangeAccount_CloseAccount()
try{
service.transferAccount()
service.changeAccountType()
service.closeAccount()
}catch(All Errors){
//find which service failed and rollback until everything backs to start state
}
We do not have any database here. These are only services... and the transaction need to be managed at the service level.
The project is based on Spring.
Can you please let me know what is the best approach for this usecase?

Related

Spring-Cloud, Hystrix and JPA - LazyInitializationException

I have the following problem trying to integrate Hystrix into an existent Spring Boot application. I am using boot with spring data (jpa repositories). The structure of the app is pretty simple,
we have Resources -> Services -> Repositories.
I enabled Hystrix support and annotated one of the service methods that returns an entity as follow:
#HystrixCommand(fallback="getDealsFallback")
public Page<Deal> getDeals(...) {
// Get the deals from the Index Server.
return indexServerRepository.findDealsBy(...);
}
public Page<Deal> getDealsFallback(...) {
// If IndexServer is down, query the DB.
return dealsRepository.findDealsBy(...);
}
So this works as expected, the real problem resides actually when I return the Entity to the client. I am using OpenEntityManagerInViewFilter so I can serialize my model with its relations.
When I use #HystrixCommand in my service method, I get a LazyInitializatioException when It tries to serialize.
I know the cause (or at least I suspect what is the problem), and is because Hystrix is executing in another thread
so is not part of the transaction. Changing the Hystrix isolation strategy from THREAD to SEMAPHORE, works correctly since its the same thread, but I understand that is not the correct way to approach the problem.
So my question is, How can I make the Hystrix executing thread be part of the transaction. Is there any workaround that I can apply?
Thanks!
It is a little old thread, but maybe someone meets this problems too. There is an issue in github about this.
The reason is, hystrix will run in separate thread, which is different from where the previous transaction is. So the transaction and serialization for lazy will not work.
And the 'THREAD' is the recommended execution strategy too. So if you want to use both hystrix and transaction, you should use them in 2 level calls. Like, in first level service function, use transaction, and in second level service function, use hystrix and call first level transactional function.

ClassBridge called by MassIndexer and access to spring service

I wonder whether it is possible to use any spring services from within hibernate search ClassBridge.
Abstract:
My entity does not have all information i'd like to put into indexed document. I can not get it by #IndexedEmbeded too. This data comes from external data source, and I have service to provide this. This is only needed when reindexing. When indexing single object save service provide this information in transient entity field.
For reindex I use MassIndexer.
My application is working in Spring MVC environment. I use annotation driven configuration. So to access my service I need only use #Autowired annotation and service is ready to be used.
Solution?
When using Hibernate Search life is not so easy (or I have no idea how it could be).
To get additional information I decided to use ClassBridge. I also implement simple MassIndexer procedure called from within my spring service.
In my ClassBridge spring does not autowire service. When I try do quick workaround and use static field in ClassBridge and pass service reference from MassIndexer caller the other problem occurred. Service reports exception "no session" (sessionFactory.getCurrentSession() throws exception)... I have no idea where to go further.
What is recommended way to access Spring service from within ClassBridge code?
How can I get active hibernate Session (or SessionFactory) from within ClassBridge?
Thanx for you time & hope your help.
The recommended way is through compile time weaving and #Configurable
A comment on this page (http://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/) provides a much simpler way of handling this that might be useful. You can have Spring autowire an object for you, more explicitly by calling:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
Doc: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

Transaction support in struts2

I am using struts2.0 for my project named online examination system.
I am using traditional JDBC approach in datya access layer. I can't use JPA.
I want to make entire service layer transactional, but i searched for the transaction support in struts2 but didn't get anything.
Although spring provides good support for making service layer transactional.
Please help me on this issue to find out transaction support in struts2.
You are on a wrong track here.Struts2 is only for MVC part and what you are doing at your service layer it has nothing to do with that.
If you want to take advantage of transaction API i suggest you to use either Spring AOP of if i am correct Google Guice also provides a way for AOP.
Whatever you use at your service layer for transaction handling Struts2 is independent of it and out of context

spring + struts2, inject DAO into external thread

I have a web application that uses Struts2 + Spring for the resource injection, basically my DAO. Now I would like to create a thread that periodically polls the database and, if needed, send email notifications to users.
I would like to know how I can implement this in a way that this thread can use my DAO. I haven't been able to manage Spring to inject it the way I've done it. So I would like to hear suggestions and see if someone can point me to the right way.
Right now I have a thread started by a ServletContextListener, that just creates a timer and schedules an action every 5 minutes. But I can't get this action to use my DAO. I don't have any need to use this structure, I'm open to using whichever solution works.
Thanks for your help!
Edit: As axtavt suggested, I used Spring task Execution Scheduling and it works perfectly, the thing is that my task gets injected with the DAO but then I get LazyInitializationException every time I try to access a property of my fetched objects, any suggestion on how to solve that??
Perhaps the best option is to use Spring's own scheduling support, see 25. Task Execution and Scheduling (if necessary - with Quartz, see 25.6 Using the OpenSymphony Quartz Scheduler). This apporach allows you to configure your scheduled action as Spring beans, so you can wire them with other beans such as DAO.
Alternatively, you can use the following to obtain any Spring bean in web application (for example, to obtain DAO from your thread):
WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(...)

Spring embedded ldap server in unit tests

I am currently trying to use an embedded ldap server for unit tests.
In Spring Security, you can quickly define an embedded ldap server for testing with the tag with some sample data loaded from the specified ldif.
I will be using Spring Ldap to perform ldap operations, and thinking of testing the usual CRUD features of my User service object.
Is there, however, a way to ensure that the entries in the embedded server to be in the same consistent state (sort of like a delete all and reload the ldif entries) for each test I am running?
I thought of the following:
1) Indicate that the method dirties the context, and force a recreation of the embedded ldap server, which sounds painful as it would have to restart the server for every method
2) Create the test entries in a test organization, such that I can unbind them and simply load in the ldif file again there.
I prefer 2, but it seems like the Spring LDAP has no good helpers to load and send across the content of a ldif file.
Any suggestions on how you perform ldap testing with an embedded ldap server of spring, or of the two possible solutions I mention?
Thanks
Doesn't Spring LDAP provide transactional control around LDAP operations? If so, why not use Spring test framework with its auto-rollback capability?
I also know of a JDBC-LDAP bridge driver that wraps an LDAP repository, presenting it as a relational database. I've used iBatis to connect to this (I've written this up at http://lokibear.blogspot.com, see articles from July). I've not yet tried applying transactional control, but the website for the driver mentions the ability to ignore transactions (which means you can also not ignore them...right?).
Like I say, I haven't tried this yet; but, if this provides transactions around LDAP, then you can again use the Spring test framework to get auto-rollback in place. I've put out a quick cheatsheet around that framework - see the September posts at my blog.
Sorry, I might be missing your goal here; but perhaps these suggestions are useful. Good luck!
I may be off-track here, but if you're not testing the LDAP integration itself, you could Mock out the LDAP connection with a Mock object that always returns the values you expect so that your other Unit Tests can complete.
If you're testing the LDAP connection then you're really doing an integration test. In which case it's probably best to connect to a real LDAP implementation.
You may or may not know that the embedded LDAP functionality is not provided by Spring LDAP itself, but Apache Directory Server. Unfortunately, the LDIF loader in Apache DS (as wired by Spring, anyway) has very poor error handling and reporting capability, and as such is probably not going to behave as you really want for a unit test. Your best bet if you really want to start from a clean slate each time is to take the lead of the Spring Security LDAP unit tests and reinitialize Apache DS every time, with a clean LDIF file load.
Alternatively, you could eschew LDIF altogether and construct your own unit test wrapper that verifies the pre- and post-conditions of the data prior to your unit tests running. This would be more work, but ultimately may work out better for you.
Works fine for me:
#Inject
private ApplicationContext applicationContext;
#Before
public void reloadLdapDirectory() throws NamingException, IOException{
ApacheDSContainer apacheDSContainer = (ApacheDSContainer) applicationContext.getBean(BeanIds.EMBEDDED_APACHE_DS);
LdapTestUtils.clearSubContexts(contextSource, DistinguishedName.EMPTY_PATH);
ClassPathResource classPathResource = new ClassPathResource("ldap.ldif");
File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
try {
InputStream inputStream = classPathResource.getInputStream();
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
LdifFileLoader fileLoader = new LdifFileLoader(apacheDSContainer.getService().getAdminSession(), tempFile.getAbsolutePath());
fileLoader.execute();
}
finally {
try {
tempFile.delete();
}
catch (Exception e) {
// Ignore this
}
}
}
I asked something similar and got an answer from Luke Taylor: Integration tests with spring-security and ldap

Resources