• Register
0 votes

Problem :

I am new to the Spring and JPA, wasted 10 days and no result with the searching on internet. I want to save the object to SQL SERVER, my connection is correct but when I write .flush() I get below exception

“nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress”


7 5 2
3,870 points

2 Answers

0 votes

Solution :

The problem is that you also scan a service and repository packages in your child context. You thus end up with below two instances of each service and repository:

· One in a main context, which is the transactional

· One in a child context, which is not the (since a child context doesn't care about transaction management)

A controller is thus injected with the service coming from a same context as a controller: the not a transactional one.

To confirm that, you should add traces in a constructor of a beans and see how many times they are getting instantiated.

And to avoid a problem, there are below two solutions:

· Avoid the scanning of repository and service packages in a mvc context: this context should only care about the mvc-related beans. When Spring injects the service in the controller, it would thus not find a service in a mvc context, and thus look it up, and find it in a main context. A transactional service would thus be the injected.

· Use the single context: the one of a servlet, where all the beans in a application would be defined.

9 7 4
38,600 points
0 votes

Suppose that you have two Spring contexts.

  • The main one is configured by jpaContext.xml, where the beans from the services and repository packaged are scanned and proxied by the transactional interceptors.
  • The MVC, that is configured by the other XML file whose role is to describe the MVC part of the application like define and configure the controller beans, the view resolver, etc. This context is the child of the main one.


The problem is that you can also scan the services and repository packages in this child context. You can end up with the two instances of each service and repository;

  • One is the main context that is transactional.
  • One is the child context that is not,

The controller is injected with the services coming from the same context as the controller.


There are two possible solutions;

  • Avoid scanning the repository and service packages in the MVC context. This context has only cared about the MVC related beans. When a service is injected by the Spring, it would not find the services in the MVC context thus look up and find in the main context. Transactional services can also be injected.
  • Use a single context,  one of the servlets, where all the beans in the application can be defined.
11 5 1
3,890 points

Related questions

0 votes
1 answer 205 views
Problem : I am new to Spring and JPA I want to save object to SQL SERVER, connection is correct but when I write .flush() I get the exception as below javax.persistence.TransactionRequiredException: no transaction is in progress
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 2.2K views
Problem : I am getting bellow error while using hibernate JPA with spring and mongodb and running my application on Glassfish-4.0. javax.persistence.transactionrequiredexception: executing an update/delete query
asked Oct 19, 2019 peterlaw 6.9k points
1 vote
1 answer 1.3K views
Problem : When I try to compile my spring project, I always face the below error : Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed I have the ... >1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> . . .
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 3.8K views
Problem : I am developing the application in Spring, using a Tomcat, Mysql5, Java8... My problem is that I cannot deploy it, due to the "required bean 'entityManagerFactory' not found" problem. I have developed my project with the coworkers but ... a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
asked Jan 25 jwilliam 3.9k points
1 vote
1 answer 1K views
Problem : I am beginner to spring + hibernate. Recently I have created the application with the use of spring + hibernate but I always face below error. I am trying to work on it for the very first time. Below is the error for your ... ) at coreservlets.StudentDAOImpl.create(StudentDAOImpl.java:19) at coreservlets.MainApp.main(MainApp.java:14) Can somebody help me in overcoming above error?
asked Mar 28 morrisBson 3.2k points