Note that there are some explanatory texts on larger screens.

plurals
  1. POTransactionRequiredException in spring webmvc framework
    text
    copied!<p>It's many similar, but not the same issues, so I couldn't find a solution for this one.</p> <p>I have Spring + JPA(Hibernate) web-application.</p> <p>Context configuration (data-context.xml):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="org.postgresql.Driver"/&gt; &lt;property name="url" value="jdbc:postgresql://localhost:5432/db"/&gt; &lt;property name="username" value="postgres"/&gt; &lt;property name="password" value="1234"/&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="emf"/&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt; &lt;/property&gt; &lt;property name="packagesToScan" value="com.myapp.mvc.logic.domain"/&gt; &lt;property name="jpaProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt; &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt; &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt; &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.myapp.mvc.logic" /&gt; </code></pre> <p></p> <p>DispetcherServlet configuration (servlet-context.xml):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd "&gt; &lt;annotation-driven validator="validator"/&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;default-servlet-handler/&gt; &lt;beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver"&gt; &lt;beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/&gt; &lt;/beans:bean&gt; &lt;beans:bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer"&gt; &lt;beans:property name="definitions"&gt; &lt;beans:list&gt; &lt;beans:value&gt;/WEB-INF/layouts/layouts.xml&lt;/beans:value&gt; &lt;beans:value&gt;/WEB-INF/views/**/views.xml&lt;/beans:value&gt; &lt;/beans:list&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;interceptors&gt; &lt;beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/&gt; &lt;/interceptors&gt; &lt;beans:bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="classpath:META-INF/i18n/application, classpath:META-INF/i18n/validation_messages" /&gt; &lt;beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"/&gt; &lt;beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"&gt; &lt;beans:property name="validationMessageSource" ref="messageSource"/&gt; &lt;/beans:bean&gt; &lt;context:component-scan base-package="com.myapp.mvc" /&gt; </code></pre> <p></p> <p>Controller:</p> <pre><code>@RequestMapping("/users") @Controller public class BankAccountCatalogController { @Inject private Functions functions; @RequestMapping(value="/update", method = RequestMethod.POST) public String catalogBankAccountUpdate(Model uiModel) { functions.addUser(); return "employee_cabinet/catalog_bank_account"; } } </code></pre> <p>Functions.class:</p> <pre><code>@Service("functions") @Repository @Transactional public class Functions { @PersistenceContext private EntityManager em; @Transactional public void addUser() { User user = new User(); user.setFirstName("John"); user.setLastName("John"); user.setMiddleName("John"); user.setEmail("John@gmail.com"); user.setPhone(null); user.setMd5Password("1234"); em.persist(user); em.flush(); } } </code></pre> <p>When I activate the controller, in <code>em.flush();</code> line I get an error:</p> <pre><code>javax.persistence.TransactionRequiredException: no transaction is in progress </code></pre> <p>On some reason transaction don't active in <code>addUser()</code> function. I tried run function in JUnit environment - it works fine. Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload