Spring upgrade transactional issue - java

I am upgrading my Spring 3 to Spring 4, and was facing issue reported in
OpenEntityManagerInViewInterceptor- No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
Now to resolve it I am putting #Transactional in my controller entry method and when I do so I am getting below exception at deployment:
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy47 implementing com.krawler.spring.accounting.payment.accPaymentDAO,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.krawler.spring.accounting.payment.accPaymentImpl' for property 'accPaymentDAOobj': no matching editors or conversion strategy found
This class only has messageSource entry for bean injection. And it is separate class from the controller.
When I remove the #Transactional from Controller method, I dont see this error.
Please let me know what can be done.

Related

LazyInitializationException occuring post introducing #Controller Annotation

We have a distributed web application implemented in Spring using Method Name Resolver. We are now migrating from Method Name Resolver to Spring Annotation (#Controller). Once we did that change, the big issue that we are facing is all the LAZY Initialization is not working. We started getting the below error:
2020-11-10 13:12:53,678 DEBUG o.s.web.servlet.DispatcherServlet:1182 Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'XXXXXXXX'; model is {XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) ~[hibernate-core-4.2.17.Final.jar:4.2.17.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) ~[hibernate-core-4.2.17.Final.jar:4.2.17.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) ~[hibernate-core-4.2.17.Final.jar:4.2.17.Final]
On Analysis, we are able to find that, with the Method Name Resolver, System was able to Lazy Initialize the object. While in the #Controller annotation, System throws the LazyInitializationError, since the Session object is getting closed and not able to lazily initialize.
What could be the solution to overcome this? how to maintain the session through with the #Controller similar to Method name resolver.
Any insights will be highly apprecitated. Thanks.
Sounds like you are using the "Open Session In View" (anti-)pattern. Look at Spring's OpenSessionInViewFilter.
Sounds like your controller method is asynchronous. If that's the case there would be no session as the call is done in a different thread and the session doesn't come along with it. This similar question explains that and has a solution.
Check if your controller method have an #Async annotation or something similar. If that's the case, it would definitely throw that error.

How can I use ApplicationContext session scoped classes from a different thread

Using #Async annotation I want to call a method in a different thread that has access to Session and Request scoped classes.
However when the ApplicationContext tries to get the bean the following exception is generated:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.sessionInfoClass': Scope 'session' is not active for the current thread;
I had tried extending ApplicationContextAware class to hold the main thread context.
Also I had tried the suggested solution from this question How to enable request scope in async task executor
Source coude is in Github
https://github.com/saavedrah/spring-threadSample
I have created a pull request for your repo that solves the issue.
Basically, I extended this solution also for Runnable case.
To verify it, run the ThreadSampleApplication class then hit http://localhost:8080/testAsync

Spring Validation with Hibernate Validation

I want to combine Hibernate and Spring Validators. I came across this link:
http://www.mkyong.com/spring-mvc/combine-spring-validator-and-hibernate-validator/
And it is working as expected. However to get it to work have to comment out InitBinder, enter the method and throw an exception within method itself if there are errors in the BindingResult.
I would prefer for it to be like #Validated where it throws the exception prior to even entering the method
#Validated is for Spring and throws MethodArgumentNotValidException if any errors come up.
Is there a way to use invoke Hibernate validate as part of Spring Validator class or call both and bind them to the same Result
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/SpringValidatorAdapter.html
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.html
Came across the above classes: Adapter that takes a JSR-303 javax.validator.Validator and exposes it as a Spring Validator while also exposing the original JSR-303 Validator interface itself.
This solved the issue. I created a SpringValidatorAdapter bean and am injected it into my Spring Validators and calling validate on the object

How to create a request-scoped bean manually?

I have a request-scoped bean which are used in app. Now I need to implement some predefined configuration beans. I tried both ways:
as a InitializingBean implementation
as a spring's ApplicationListener<ApplicationReadyEvent> listener
but the problem is that code within this config beans uses erquest-scoped bean and everytime I get a:
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread?
is there any way of simulating request?

Java JPA annotation #TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) not working as expected

Running J2EE application on JBoss 5.1 using Hibernate. I need to call some methods (in quartz scheduler) that use explicit database connections from an enterprise bean. Currently get an error about calling commit not allowed in a transaction.
I was hoping that adding the #TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) annotation to the bean method would allow me to call quartz api, but still get the same exception. Am I misunderstanding what this annotation means, or is something else going on?
2014-06-23 17:43:41,184 ERROR [http-0.0.0.0-8443-4] org.quartz.JobPersistenceException: Couldn't commit jdbc connection. You cannot commit during a managed transaction! [See nested exception: java.sql.SQLException: You cannot commit during a managed transaction!]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.commitConnection(JobStoreSupport.java:3661)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3799)
at org.quartz.impl.jdbcjobstore.JobStoreTX.executeInLock(JobStoreTX.java:93)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeWithoutLock(JobStoreSupport.java:3703)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.checkExists(JobStoreSupport.java:1897)
at org.quartz.core.QuartzScheduler.checkExists(QuartzScheduler.java:1491)
at org.quartz.impl.StdScheduler.checkExists(StdScheduler.java:555)
Some more data:
1) If I call my managed bean method from a non-managed class without the annotation, then I get the same exception (transaction is created)
2) If I call my managed bean method from a non-managed class with the annotation then I dont get the exception (no transaction created)
3) If I call my managed bean method from another managed bean or from itself, then I get the exception with or without the annotation (transaction exists)
So the annotation does seem to prevent a new transaction being created, but it does not seem to suspend an already existing transaction (or maybe I am getting this all wrong).

Categories

Resources