I'm currently experiencing problems with a JPATransactionManager in a J2SE application.
The latest request was not properly saved in the database (surely a problem of cache).
This seems to be normal because the exit of the program does not seem to "cleanly" exit all Bean of the spring application context.
Do you know if there's a method of a clean shutdown a Spring context before exiting ?
Thank you in advance.
Checkout Spring Reference manual on this topic.
Basically you need to call registerShutdownHook() on the application context.
Related
I need to create logging (logging and entry in a DB table), with the cause or exception occurred if a running spring application is stopped abrubtly or due to any server exception occurred. Is there any concept or module in spring which I can use for this purpose?
Please help if anyone knows or have done the same thing, thanks in advance!!
You can implement EventListener for ApplicationContextStoppedEvent and there you can add your logic
I have a Spring Boot 2 application (still in the development stage) running well with JPA, hibernate and so on. The single persistence test I've got right now goes thru fine.
However, when I add #EnableBatchProcessing to the main boot class (the one annotated with #SpringBootApplication) I get the following error during the test phase of the maven build:
javax.persistence.TransactionRequiredException: no transaction is in
progress
If I just remove this annotation the test runs successfully again.
I read somewhere that spring Batch uses a different transaction manager than that used for JPA persistence.
How can I fix this issue?
Thanks in advance.
This is very simple.#EnableBatchProcessing annotation causes Spring Batch to automatically register a transaction manager to use for its transactions, and your JpaTransactionManager never gets used.
Reason:
By default, #EnableBatchProcessing triggers the creation of a DataSourceTransactionManager. This transaction manager knows nothing about JPA/Hibernate which causes the problem you're seeing.
Solution:
Now, if you want to change the transaction manager that Spring Batch uses for transactions, you have to implement the interface BatchConfigurer. There is a link to an example where a user has done this. What he is doing is switching the transaction manager to his own transactionmanager.
I am developing a java web application in which I want to cache all the data present in a table during server start up.
Also if there are any changes in DB values, I wish to refresh the cache (without restarting the server).
I am looking for some material in spring which may help me in achieving that. But I am not able to figure it out.
Please help how can I achieve the same. Also I would like to initialize some beans on server start up.
To start with read the following docs which will get you started.
Refer to Spring document http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/cache.html
Also check this simple tutorial http://viralpatel.net/blogs/cache-support-spring-3-1-m1/
Regarding your bean initialization you can use #PostConstruct annotation on a method of the bean class. Spring will call that method after the bean is constructed.
The application that your planning to build wouldnt be an easy one. In my experience, creating an application like that would require a knowledge of the following:
1. Spring
2. Ehcache
3. JMX
4. Servlet Listeners
I am using the following versions:
Spring 3.1.1.RELEASE
Netty 3.4.0.Final
Hibernate 3.5.6-Final
Now, I have a Netty server that works fairly well - the root of the server, the pipeline factories and the base "stub" of the server that owns everything are all set up with Spring. In this stub, spring #Transactional annotations work just fine.
However, in the handlers, which are stateful and created dynamically depending on what state the user is in - #Transactional doesn't work. I'm fairly sure I understand why. I even have a "solution" - but it's not very good.
After the decoders and encoders, I add an ExecutionHandler:
pipeline.addLast("execution", new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16,1000000, 1000000)));
This appears to be where the Spring transaction support is breaking. Since Spring is unaware of these threads, it can't bind any transactions to them. The classes are proxied correctly, but in debug they have no associated transactions.
My solution is crappy, and it needs to be replaced by a real solution:
Session sess = SessionFactoryUtils.getSession(getSessionFactory(), true);
That's bad because it relies on me to release the session, and it may not even be transactional, I haven't checked. It sucks in a lot of ways.
Anyway - the root of the question. Given the above tech, what's my path to getting my #Transactional notations working on the Netty handlers?
Write an ExecutionHandler that's Spring aware?
NOTE: I can't upgrade to Hibernate 4, due to lack of compatibility with Spring-Flex, used in another project in the group. Probably the same story for the Spring version, can't remember.
I suggest you create these netty's handler inside spring container and inject the service or persistence layer into the handlers so you can have these layers independence from netty and of course these are old school spring beans.
From my experience of using spring MVC in conjunction with hibernate I know about lazy exception problem occured when addressing to lazy-fetched collection when rendering view.
It pure spring+hibernate world it fixes by introducing OpenSessionInViewInterceptor or OpenSessionInViewFilter thus enabling one hibernate session per request.
So the question is: should I care about this problem in grails or such one-session-per-request behaviour is enabled by default.
If this isn't grails defaults please provide some code to implement this behaviour.
Thanks.
Grails registers a customized subclass of OpenSessionInViewInterceptor (it adds WebFlow awareness). So anything done in the context of a web request will have an open session and lazy-loaded references and collections will resolve.