Spring JPA TransactionRequiredException:没有事务处于进程中

时间:2012-12-07 15:21:48

标签: spring hibernate jpa

所以整天都在花费JPA工作,没有成功也许你会看到我错过的东西。

我将发布我的配置

appContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <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 ="pwd"/>
    </bean>


 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    <property name="persistenceUnitName" value="hibernatePersistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
            <property name="showSql" value="true"/>
        </bean>
    </property>
 </bean> -->



<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> 

    <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" proxy-target-class="true"/>

我的Chain.java实体

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

      private Long id;
      private String name;



@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator ="Chain_id_seq")
@SequenceGenerator(name="Chain_id_seq", sequenceName="Chain_id_seq")
public Long getId(){ return id;}
public void setId(Long id){ this.id = id;}

@Column(name = "name")
public String getName(){return name;}
public void setName(String value) {this.name = value;}

我的ChainServiceImpl.java类

@Service("addChainService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ChainServiceImpl implements ChainService{

    @Autowired
    private ChainDao chainDao;


    @Autowired
    public ChainServiceImpl(ChainDao ChainDao) {
    this.chainDao = ChainDao;
    }

    public ChainServiceImpl() {
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int addCaChain(Chain chain) {
        int id  = chainDao.saveCaChain(chain);
        return id;
    }
}

我的ChainDaoImpl.java类

@Repository("chainDao")
public class ChainDaoImpl implements ChainDao{

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this. entityManager = em;
    }




    public int saveChain(Chain chain) {

        entityManager.persist(chain); //em.merge(u); for updates
        entityManager.flush();
        return 9;
    } 
}

异常追踪

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:733) ~[hibernate-entitymanager-3.5.0-Final.jar:3.5.0-Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_09]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_09]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_09]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_09]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) ~[spring-orm-3.1.3.RELEASE.jar:3.1.3.RELEASE]
    at $Proxy18.flush(Unknown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_09]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_09]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_09]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_09]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) ~[spring-orm-3.1.3.RELEASE.jar:3.1.3.RELEASE]
    at $Proxy18.flush(Unknown Source) ~[na:na]
    at pl.signet.famoc.postgresql.jpa.dao.CaChainDaoImpl.saveCaChain(CaChainDaoImpl.java:47) ~[CaChainDaoImpl.class:na]
    at pl.signet.famoc.postgresql.jpa.service.SignetCAStorageChainServiceImpl.addCaChain(SignetCAStorageChainServiceImpl.java:33) ~[SignetCAStorageChainServiceImpl.class:na]
    at pl.signet.famoc.ws.spring.endpoint.FamocCaEndpoint.getCAChain(FamocCaEndpoint.java:42) ~[FamocCaEndpoint.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_09]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_09]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_09]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_09]
    at org.springframework.ws.server.endpoint.MethodEndpoint.invoke(MethodEndpoint.java:134) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter.invokeInternal(DefaultMethodEndpointAdapter.java:229) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.endpoint.adapter.AbstractMethodEndpointAdapter.invoke(AbstractMethodEndpointAdapter.java:53) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:233) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:173) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.java:88) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:59) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:221) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) [servlet-api.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.30.A]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.30.A]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.30.A]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.30.A]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.30.A]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) [tomcat-coyote.jar:7.0.30.A]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) [tomcat-coyote.jar:7.0.30.A]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) [tomcat-coyote.jar:7.0.30.A]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_09]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_09]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_09]

当我不使用flush时,异常出现在行entityManager.flush()中,没有异常,但数据库没有任何异常,没有插入记录。

有人知道我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

我认为你的appContext.xml需要一个<context:component-scan><context:annotation-config>仅激活已在应用程序上下文中注册的bean的注释。 component-scan都扫描bean的包以注册并激活注释。由于您没有component-scan@Transactional注释不会将该方法包装在事务中。

关于component-scann和annotation-config之间差异的好post