Spring Boot: javax.persistence.TransactionRequiredException: Executing an update/delete query - spring-boot

I am using Spring Boot and configured my Application like this:
#Configuration
#EnableAutoConfiguration
#EnableTransactionManagement
#ComponentScan
#EntityScan("ch.xy.model")
public class Application {
#Autowired
private ImportDAO importDao;
}
ImportDAO looks like that:
#Repository
public class ImportDAO {
#PersistenceContext
private EntityManager em;
#Transactional
void removeTempoAccounts() {
Query q = em.createQuery("DELETE FROM TempoAccount t WHERE t.manual = false");
q.executeUpdate();
}
}
But when removeTempoAcconts is executed I get:
Exception in thread "main" javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:360)
at com.sun.proxy.$Proxy49.executeUpdate(Unknown Source)
at ch.post.pf.jira.tempocats.pspimport.ImportDAO.removeTempoAccounts(ImportDAO.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at ch.post.pf.jira.tempocats.pspimport.ImportDAO$$EnhancerBySpringCGLIB$$1883cf82.removeTempoAccounts(<generated>)
at ch.post.pf.jira.tempocats.pspimport.PspImport.run(PspImport.java:32)
at ch.post.pf.jira.tempocats.pspimport.Application.main(Application.java:20)
What's wrong with my configuration?

#Transactional
void removeTempoAccounts() {
Method had default visibility. Therefore the proxy mechanism was not active!
After changing to public everthing works as expected!

Try to use the #Transactional annotation from Spring package
like #org.springframework.transaction.annotation.Transactional
Then it should work

Related

Using #Transactional and #PostConstruct on same method

I tried to initialize my database using code bellow
#Component
public class DataBase {
#Transactional
#PostConstruct
public void init() {
Query nativeQuery = entityManager.createNativeQuery("Native SQL Query");
nativeQuery.executeUpdate();
}
}
and I got this runtime exception
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1496) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:380) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
but injecting DataBase and calling init function is working fine!
#Component
public class TestClass {
#Autowired
private DataBase dataBase;
#PostConstruct
public void test() {
dataBase.init();
}
}
why this is happening!,is this a bug or there is some reason?

Spring #Scheduled method Cannot Save with Spring Data JPA

SpringBoot 1.4.1.RELEASE is used. A simple task is create that runs every 5 seconds. It fetched data from database using Spring Data JPA, sends the data through an API and on success, updates send status in database using Spring Data JPA.
Following is scheduler code snippet
#Component
public class MemberJob {
#Value("${base.url}")
private String baseApiUrl;
#Autowired
private RestTemplate restTemplate;
#Autowired
#Qualifier("memberServiceBean")
private EntityService memberServiceBean;
#Scheduled(fixedDelay = 5000l)
public void runJob() throws Exception {
Collection<Member> notSynched = memberRepository.findAll();
notSynched.stream().forEach(m -> {
//Send to server: on success
m.setSyncStatus(true);
memberServiceBean.update(m);
});
}
}
The problem is on success the member is not updated.For some reasons Spring Data JPA is not working in #Scheduled method. How can i make data jpa update this object inside self-invocation #Scheduled method?
And yes the update method in service layer is decorated with #Transactional like so
#Transactional(propagation = Propagation.REQUIRED)
public Member update(Member m) {
return (Member) getRepository().save(m);
}
Update
Based on suggestions on comment section, update method is surrounded by try/catch unfortunate no exception was caught! Like so
try{
m = (Member) memberServiceBean.update(m);
}catch(Exception ex){
ex.printStackTrace();
}
Also in application.properties file, logging.level.org.springframework.transaction.interceptor=TRACE was added and following trace seen
2017-10-07 11:23:21.968 TRACE 2200 --- [ask-scheduler-3] o.s.t.i.TransactionInterceptor : Getting transaction for [re.iprocu.service.impl.MemberServiceBean.update]
2017-10-07 11:23:21.969 TRACE 2200 --- [ask-scheduler-3] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.QueryDslJpaRepository.save]
2017-10-07 11:23:22.536 TRACE 2200 --- [ask-scheduler-3] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.QueryDslJpaRepository.save]
2017-10-07 11:23:22.536 TRACE 2200 --- [ask-scheduler-3] o.s.t.i.TransactionInterceptor : Completing transaction for [re.iprocu.service.impl.MemberServiceBean.update]
Still no update to database is realized. If any additional information needed, please don't hesitate to ask.
Update 2
Respository save if changed to saveAndFlush following exception is caught
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:413)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy209.saveAndFlush(Unknown Source)
at re.iprocu.service.impl.AbstractFacade.update(AbstractFacade.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy237.update(Unknown Source)
at re.iprocu.job.iprocuredata.MemberJob.lambda$runJob$0(MemberJob.java:104)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at re.iprocu.job.iprocuredata.MemberJob.runJob(MemberJob.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1136)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1297)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy171.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy171.flush(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.flush(SimpleJpaRepository.java:553)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:521)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 38 more
I had the same issue. Resolved it with including something like this in my #Repository interface:
#Modifying
#Query("update coop_member set sync_status = :syncStatus where member_id = :memberId")
void updateSyncStatus(Long memberId, boolean syncStatus);
Variable names and types could be different, but the important part was using the #Modifying and the #Query annotations.
It also assumes marking with #Transactional and #EnableScheduling. None of the entity manager flushing/merging or setting propagation/isolation on the transaction worked. Call that method instead of save().
As there is no answer that fixed my saving issue in #Scheduler self-invocation method, i decided to use JdbcTemplate which does work.
jdbcTemplate.update("UPDATE coop_member SET sync_status = ? WHERE member_id = ?", uMember.getSyncStatus(), uMember.getMemberId());
In my case i only want to update sync_status so it not alot of work.

With Spring 4, how do I create #Transactional outside of my main thread?

I'm using Spring 4.3.8.RELEASE with Hibernate 5.1. I have a thread pool set up in my context
<bean id="myprojectThreadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
<constructor-arg value="myproject-"/>
</bean>
<bean id="myprojectTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="threadFactory" ref="myprojectThreadFactory"/>
<property name="corePoolSize" value="${myproject.core.thread.pool.size}" />
<property name="maxPoolSize" value="${myproject.max.thread.pool.size}" />
</bean>
But how do I create a transaction within one of my task threads, which is outside of my main thread? I'm trying this
// Cue up threads to execute
for (final Organization org : allOrgs)
{
m_threadExecutor.execute(new Thread(new Runnable(){
#Override
#Transactional(propagation=Propagation.REQUIRES_NEW)
public void run()
{
System.out.println("started.");
processData(org.getId());
System.out.println("finished.");
}
}));
but this is resulting in the below exception
Exception in thread "myproject-1" Exception in thread "myproject-2" org.hibernate.SessionException: Session is closed!
at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:132)
at org.hibernate.internal.SessionImpl.setCacheMode(SessionImpl.java:1511)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1109)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1033)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy68.find(Unknown Source)
at org.mainco.subco.core.repo.GenericDao.find(GenericDao.java:123)
at org.mainco.subco.organization.repo.OrganizationDaoImpl.find(OrganizationDaoImpl.java:61)
at org.mainco.subco.organization.service.OrganizationServiceImpl.getDescendantOrganizations(OrganizationServiceImpl.java:283)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy75.getDescendantOrganizations(Unknown Source)
at org.mainco.subco.myproject.repo.myprojectClassDaoImpl.findBymyprojectOrg(myprojectClassDaoImpl.java:89)
at org.mainco.subco.myproject.service.myprojectClassServiceImpl.find(myprojectClassServiceImpl.java:300)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy91.find(Unknown Source)
at org.mainco.subco.myproject.quartz.ImportClassesWorker.preProcessData(ImportClassesWorker.java:56)
at org.mainco.subco.myproject.quartz.AbstractImportDataWorker.processData(AbstractImportDataWorker.java:126)
at org.mainco.subco.myproject.quartz.AbstractImportDataWorker.access$0(AbstractImportDataWorker.java:123)
at org.mainco.subco.myproject.quartz.AbstractImportDataWorker$1.run(AbstractImportDataWorker.java:110)
at java.lang.Thread.run(Thread.java:745)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Use TransactionTemplate for the logic. Something like this
final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
taskExecutor.execute(new Runnable() {
#Override
public void run() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
#Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
System.out.println("started.");
processData(org.getId());
System.out.println("finished.");
}
});
}
});
Spring doesn't know anything about your Runnable instance. Therefore adding annotation on the run method doesn't make any sense. Instead what you want to do is to put #Transactional on processData method. But note that it should be the method of a Spring bean!
UPDATE:
Just tried to mark a business method with both #Transactional and #Async and it worked for me. I used spring-boot of version 1.5.4.RELEASE that comes with spring 4.3.9.RELEASE and hibernate 5.0.12.Final. For my #Async configuration, I just used #EnableAsync. Here what I've got:
#Test
#Transactional
public void test() throws InterruptedException {
beanA.testMethodA();
//Sleeping, since after test is completed application is down which leaves #Async transaction not completed.
Thread.sleep(3000);
System.out.println("Thread in test method: " + Thread.currentThread().getName());
System.out.println("Transaction in test method: " + TransactionSynchronizationManager.getCurrentTransactionName());
}
Here is a method of BeanA. It is transactional with a default propagation level which makes it bounded to a transaction started by the test:
#Override
#Transactional
public void testMethodA() {
beanB.testMethodB();
System.out.println("Thread in method A: " + Thread.currentThread().getName());
System.out.println("Transaction in method A: " + TransactionSynchronizationManager.getCurrentTransactionName());
}
And the last part - method of a BeanB which is marked as #Async and #Transactional. It does run in a separate thread and in a separate transaction:
#Async
#Transactional
#Override
public void testMethodB() {
System.out.println("Thread in method B: " + Thread.currentThread().getName());
System.out.println("Transaction in method B: " + TransactionSynchronizationManager.getCurrentTransactionName());
}
After I run the test I get the following output:
Thread in test method: main
Transaction in test method: com.example.stackoverflow.TransactionTest.test
Thread in method A: main
Transaction in method A: com.example.stackoverflow.TransactionTest.test
Thread in method B: SimpleAsyncTaskExecutor-1
Transaction in method B: com.example.stackoverflow.BeanBImpl.testMethodB
Note the last two statements. The method marked as #Async and #Transactional is running in its own transaction and its own thread.

Spring mvc junit test serrice

I'm using spring mvc.
I want to test my service with Junit.
I want to test the inclusion of a new user
I write the class test:
#ContextConfiguration("/dispatcher-servlet.xml")
public class ServizioUtenteTest {
#Autowired
private ServizioUtente servizioutente;
#Test
public void testAggiungiUtente() {
//fail("Not yet implemented");
Utente utente=new Docente();
utente.setCognome("Professore");
utente.setPassword("tetxts");
utente.setUsername("xxxx");
servizioutente.aggiungiUtente(utente);
}
#Test
}
When I run the test I get:
java.lang.NullPointerException
at org.rol.test.ServizioUtenteTest.testAggiungiUtente(ServizioUtenteTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
....)
The error is thrown on the line, why?
help me!?
You are missing SpringJUnit4ClassRunner:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("/dispatcher-servlet.xml")
public class ServizioUtenteTest {
//...
See also
Creating unitary tests in Spring 3
11. Testing

Getting java.io.NotSerializableException with a spring service when stopping tomcat

i am using spring 3 with JSF 2
and i replaced JSF managed beans with spring beans, by adding on top of bean:
#Component("mybean")
#Scope("session")
and in my bean i am autowiring a spring service (which was declared with the annotation #service)
well, everything works fine with the service, but when i tried to stop tomcat 6, i am getting this exception with my spring service
java.io.NotSerializableException
any ideas why i am getting this exception, and how to solve it.
UPDATE:
after making my service implements serializable, sometimes i am getting following exception:
java.lang.IllegalStateException: Cannot deserialize BeanFactory with id org.springframework.web.context.WebApplicationContext:/spring_faces: no factory registered for this id
at org.springframework.beans.factory.support.DefaultListableBeanFactory$SerializedBeanFactoryReference.readResolve(DefaultListableBeanFactory.java:972)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1061)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1762)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)
at org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor.readObject(AbstractBeanFactoryPointcutAdvisor.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)
at org.springframework.aop.framework.AdvisedSupport.readObject(AdvisedSupport.java:550)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1496)
at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:998)
at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:394)
at org.apache.catalina.session.StandardManager.load(StandardManager.java:321)
at org.apache.catalina.session.StandardManager.start(StandardManager.java:648)
at org.apache.catalina.core.ContainerBase.setManager(ContainerBase.java:446)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4631)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
please advise.
Looking at the error, It looks like tomcat is trying to serialize the entire spring context, I think it might be due to the fact that your are implementing Serializable on Service. I was thinking may be you need to do this, I don't know JSF that well but my understanding of scoped beans makes me think, You might need some thing like this
#Component
#Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="session")
public class MyBean implements Serializable
{
//no need to implement serializable on service
#Autowired(required=true)
private MyService service;
}
You get this exception because the class does not implement Serializable. Tomcat serializes currently running HttpSession objects including all its attributes to disk whenever it is about to restart/redeploy, so that they are revived after the cycle. Session scoped objects are stored as attributes of the HttpSession, hence they need to implement Serializable to survive the restart/redeploy as well.
If you would like to make them Serializable, then implement it accordingly:
public class YourSpringService implements Serializable {}
Otherwise, just ignore the exception or turn off serializing sessions to disk in Tomcat's config.
Note that the same story also applies to JSF view/session scoped beans.
Well, finally i was able to make it work fine as follows:
1- The Service:
#Service
#Scope("singleton")
public class PersonService{
}
2- The Spring Managed Bean:
#Component("person")
#Scope("session")
public class PersonBean implements Serializable{
#Inject
private PersonService personService;
}
waiting for your feedback.
I had to use readObject() + static ApplicationContext hack to solve the issue:
#Component
#Scope(value = SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class FlashMessages implements Serializable {
#Autowired
transient private SomeBean someBean;
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
// restore someBean field on deserialization
SpringUtils.getContext().getAutowireCapableBeanFactory().autowireBean(this);
}
}
#Component
public class SpringUtils implements ApplicationContextAware {
static ApplicationContext applicationContext;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
public static ApplicationContext getContext() {
return applicationContext;
}
}

Resources