简体   繁体   English

交易需要接收,没有正在进行的交易

[英]Transactionrequiredexception, no transaction in progress

I've been currently working on a Maven + Spring + Hibernate project. 我目前正在从事Maven + Spring + Hibernate项目。 Actually, this is just a test project just to get familiar on how Spring works with Hibernate (+Maven). 实际上,这只是一个测试项目,只是为了熟悉Spring如何与Hibernate(+ Maven)结合使用。 I've already setup and prepare the necessary dependencies. 我已经设置并准备了必要的依赖项。 ie the appcontext.xml for Spring, the persistence.xml for Hibernate, the entity and DAO objects for JPA/Persistence/Hibernate. 例如,Spring的appcontext.xml,Hibernate的persistence.xml,JPA / Persistence / Hibernate的实体和DAO对象。

During debug, persist method throws Transactionrequiredexception, no transaction in progress. 在调试期间,persist方法将抛出Transactionrequiredexception,没有正在进行的事务。 I don't know what's causing this 我不知道是什么原因造成的

web.xml web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
        /WEB-INF/applicationContext-datasource.xml
        /WEB-INF/applicationContext.xml          
    </param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

application-servlet.xml 应用servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />



<context:component-scan base-package="com.names.home" />


<!-- Default locale set -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

<!-- Tell Spring to not try to map things in these directories to controllers -->
<!-- Order must be set to supercede the handler configured by the mvc:annotation-driven annotation -->
<mvc:resources order="-10" location="/img/" mapping="/img/**" />
<mvc:resources order="-10" location="/css/" mapping="/css/**" />
<mvc:resources order="-10" location="/js/" mapping="/js/**" />
<mvc:resources order="-10" location="/fonts/" mapping="/fonts/**" />
<mvc:resources order="-10" location="favicon.ico" mapping="favicon.ico" />
<mvc:resources order="-10" location="robots.txt" mapping="robots.txt" />

</beans>

applicationContext-datasource.xml 的applicationContext-datasource.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- The "webDS" data source is the main data source for names. It is referenced and
     should be configured via JNDI in your particular environment. -->

<jee:jndi-lookup id="datasource" jndi-name="jdbc/web"/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

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

<context:annotation-config/>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory" >
    <property name="persistenceUnitName" value="wzpu"/>
    <property name="dataSource" ref="datasource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

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

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

admincontroller.java admincontroller.java

package com.names.home;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class AdminController {

@Resource(name="profiledao")
protected ProfileDao profiledao;

@RequestMapping(value="/admin/insert")
@Transactional
public String insert(){
    Profile pr = new Profile();
    pr.setId(1);
    profiledao.save(pr);
    return "home";
}
}

ProfileDao.java ProfileDao.java

package com.names.home;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@Repository
public class ProfileDao {

@PersistenceContext(unitName = "wzpu")
protected EntityManager em;

public Profile find(){
    return this.em.find(Profile.class, 2);
}

public void save(Profile profile){
    this.em.persist(profile);
    em.flush();
}
}

Profile.java Profile.java

package com.names.home;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "profile")
public class Profile implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@Column(name = "PROFILE_ID",nullable=false,unique=true)
protected int id;

public Profile() {
    super();
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + id;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Profile other = (Profile) obj;
    if (id != other.id)
        return false;
    return true;
}

}

Please help me resolving this 请帮我解决这个问题

The root of your issue is that the bean you've annotated with the @Transactional annotation is not being picked up by the context that contains the tx:annotation-driven post processor. 问题的根源在于,使用@Transactional批注进行批注的bean不会被包含tx:annotation-driven后处理器的上下文拾取。 You have a couple options. 您有两种选择。 Move the @Transactional to a bean that is loaded by the context that contains the tx:annotation-driven post processor or move the tx:annotation-driven post processor into the same context that the @Transactional bean is being loaded. @Transactional移到由包含tx:annotation-driven后处理器的上下文加载的bean,或者将tx:annotation-driven后处理器移至与@Transactional Bean加载相同的上下文中。

The answer here is a similar situation. 答案在这里也是类似的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.persistence.TransactionRequiredException:Spring 5 中没有正在进行的事务 - javax.persistence.TransactionRequiredException: no transaction is in progress in Spring 5 错误javax.persistence.TransactionRequiredException:没有事务正在进行 - Error javax.persistence.TransactionRequiredException: no transaction is in progress TransactionRequiredException:Spring Data JPA正在进行任何事务 - TransactionRequiredException: no transaction is in progress with Spring Data JPA 没有正在进行的交易; 嵌套异常是 javax.persistence.TransactionRequiredException: no transaction is in progress - No transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress 尝试使JPA / Hibernate与REST配合使用— TransactionRequiredException:没有事务正在进行中错误 - Trying to Get JPA/Hibernate working with REST — TransactionRequiredException: No Transaction is in progress error Hibernate JPA和Spring javax.persistence.TransactionRequiredException:没有事务正在进行中 - Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress javax.persistence.TransactionRequiredException:@Lock 注释上没有正在进行的事务 - javax.persistence.TransactionRequiredException: no transaction is in progress on @Lock annotation javax.persistence.TransactionRequiredException的原因是什么:没有事务在进行中 - What are the causes for javax.persistence.TransactionRequiredException: no transaction is in progress Spring集成:javax.persistence.TransactionRequiredException:没有事务在进行中 - Spring Integration: javax.persistence.TransactionRequiredException: no transaction is in progress Hibernate 5.2.10.Final - javax.persistence.TransactionRequiredException:没有事务正在进行中 - Hibernate 5.2.10.Final - javax.persistence.TransactionRequiredException: no transaction is in progress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM