Hibernate JPA和Spring javax.persistence.TransactionRequiredException:没有事务正在进行中(2)

时间:2014-03-06 14:29:36

标签: java spring hibernate jpa transactional

同样,同样的问题,但相关帖子中给出的其他答案对我没有帮助。

Maven格式的源代码位于:

https://github.com/jjYBdx4IL/my.bugs.smtpserver

git clone git@github.com:jjYBdx4IL/my.bugs.smtpserver.git

“mvn test”期间抛出异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:334)
    at com.sun.proxy.$Proxy30.flush(Unknown Source)
    at my.bugs.smtpserver.MessageHookImpl.storeEMail(MessageHookImpl.java:46)
    at my.bugs.smtpserver.MessageHookImpl.onMessage(MessageHookImpl.java:30)
    at org.apache.james.protocols.smtp.core.DataLineMessageHookHandler.processExtensions(DataLineMessageHookHandler.java:128)
    at org.apache.james.protocols.smtp.core.DataLineMessageHookHandler.onLine(DataLineMessageHookHandler.java:76)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:84)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:67)
    at org.apache.james.protocols.smtp.core.SeparatingDataLineFilter.onBodyLine(SeparatingDataLineFilter.java:111)
    at org.apache.james.protocols.smtp.core.SeparatingDataLineFilter.onLine(SeparatingDataLineFilter.java:69)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:84)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:67)
    at org.apache.james.protocols.smtp.core.esmtp.MailSizeEsmtpExtension.onLine(MailSizeEsmtpExtension.java:151)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:84)
    at org.apache.james.protocols.smtp.core.DataCmdHandler$DataLineFilterWrapper.onLine(DataCmdHandler.java:67)
    at org.apache.james.protocols.netty.LineHandlerUpstreamHandler.messageReceived(LineHandlerUpstreamHandler.java:50)
    at org.jboss.netty.handler.timeout.IdleStateHandler.messageReceived(IdleStateHandler.java:270)
    at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(ChunkedWriteHandler.java:143)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:327)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:305)
    at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:207)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:343)
    at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:274)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:194)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

您的交易方法storeEMail正由onMessage直接调用。事务行为由包装storeEMail的AOP代理实现。由于您直接调用storeEMail,因此不会调用AOP代理,也没有事务。需要在注入的bean引用上调用storeEMail。您可能想重新考虑您的设计。