gpt4 book ai didi

java - spring webmvc框架中的TransactionRequiredException

转载 作者:行者123 更新时间:2023-12-02 07:08:36 35 4
gpt4 key购买 nike

有很多相似但不相同的问题,所以我找不到解决这个问题的方法。

我有 Spring + JPA(Hibernate) Web 应用程序。

上下文配置(data-context.xml):

<?xml version="1.0" encoding="UTF-8"?>
<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">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/db"/>
<property name="username" value="postgres"/>
<property name="password" value="1234"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.myapp.mvc.logic.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

<context:annotation-config/>

<context:component-scan base-package="com.myapp.mvc.logic" />

DispetcherServlet 配置(servlet-context.xml):

<?xml version="1.0" encoding="UTF-8"?>
<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
">

<annotation-driven validator="validator"/>

<resources mapping="/resources/**" location="/resources/" />

<default-servlet-handler/>

<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</beans:bean>

<beans:bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/layouts/layouts.xml</beans:value>
<beans:value>/WEB-INF/views/**/views.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>

<interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
</interceptors>

<beans:bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
id="messageSource"
p:basenames="classpath:META-INF/i18n/application, classpath:META-INF/i18n/validation_messages"
/>
<beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver"
p:cookieName="locale"/>

<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<beans:property name="validationMessageSource" ref="messageSource"/>
</beans:bean>

<context:component-scan base-package="com.myapp.mvc" />

Controller :

@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";
}
}

函数.类:

@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();
}
}

当我激活 Controller 时,在 em.flush(); 行中出现错误:

javax.persistence.TransactionRequiredException: no transaction is in progress

由于某种原因,事务在 addUser() 函数中未激活。我尝试在 JUnit 环境中运行函数 - 它工作正常。有什么想法吗?

最佳答案

尝试从 Controller 中删除 EntityManager 并将该方法指定为 Controller 上的事务性方法。

@RequestMapping("/users")
@Controller
public class BankAccountCatalogController {

@Inject
private Functions functions;

@RequestMapping(value="/update", method = RequestMethod.POST)
@Transactional
public String catalogBankAccountUpdate(Model uiModel) {
functions.addUser();
return "employee_cabinet/catalog_bank_account";
}
}

还要确保在您要扫描组件的包中指定 Functions 类,即 com.dominform.mvc.logic

<context:component-scan base-package="com.dominform.mvc.logic" />

关于java - spring webmvc框架中的TransactionRequiredException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15804518/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com