我正在开发一个项目,我有一个基于RMI的市场,我正在添加一个数据库来保存市场上的数据。
当我试图从客户端发售“item”时,我从“market”(服务器)调用名为“putItemOnSale”的远程方法,我得到一个javax.persistence.RollbackException。以下是相关方法和项目类的代码:
@Override
public void putItemOnSale(String name, String trader, Float price) throws RemoteException, RejectedException {
EntityTransaction transaction = null;
try {
transaction = beginTransaction();
List<ItemImpl> existingItems = em.createNamedQuery("findItemsWithName", ItemImpl.class).
setParameter("name", name).getResultList();
if (!existingItems.isEmpty()) {
// item already existing, error
throw new RejectedException("Rejected: Item with name : " + name + " already exists");
}
List<TraderImpl> existingTraders = em.createNamedQuery("findTraderWithName",TraderImpl.class).
setParameter("ownername",trader).getResultList();
if (existingTraders.isEmpty()){
throw new RejectedException("Rejected : Trader not registered");
}
// create Item.
ItemImpl item = new ItemImpl(name,existingTraders.get(0),price);
em.persist(item);
}
finally {
commitTransaction(transaction);
}
}
项目类:
public class ItemImpl extends UnicastRemoteObject implements Item, Serializable
{
@Id
@Column( name = "id", nullable = false )
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private long ItemId;
@Column(name = "name")
private String name;
@ManyToOne
private TraderImpl owner;
@Column(name = "price")
private Float price;
...
}
在尝试销售商品之前,我设法从客户端注册交易商,并将其添加到数据库中,没有任何异常。这是我得到的异常的完整堆栈跟踪,它对我来说并不是很明确。有没有人知道如何找到这种异常发生的原因? 我试图添加一个断点来更准确地查看问题的来源,但是一旦我尝试进入PutItemOnSale方法就会发生异常......
Exception in thread "AWT-EventQueue-0" javax.persistence.RollbackException: Transaction rolled back because transaction was set to RollbackOnly.
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:141)
at marketplace.MarketPlaceImpl.commitTransaction(MarketPlaceImpl.java:351)
at marketplace.MarketPlaceImpl.putItemOnSale(MarketPlaceImpl.java:181)
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:601)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
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:722)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy6.putItemOnSale(Unknown Source)
at client.ClientForm.sellButtonActionPerformed(ClientForm.java:1007)
at client.ClientForm.access$200(ClientForm.java:31)
at client.ClientForm$4.actionPerformed(ClientForm.java:321)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:0)
注意你如何在finally子句中提交事务(假设commitTransaction()方法的命名真的不太好):这意味着你的代码将尝试总是提交事务,即使发生异常并且事务应该是回滚。
所以你真正的问题是:你在一个finally子句中跳过一个异常,这是一个早期异常的结果,因此使早期的异常不可见。
您应该对该commitTransaction()方法添加一个检查,以查看该事务是否标记为回滚 - 如果是,则执行回滚而不是提交。