我有一个简单的Web服务,有两个操作,一个调用数据库中的存储过程来插入数据,另一个只返回输入的文本(用于测试)。我已经部署了返回输入文本的Web服务和操作,但是当我尝试测试“调用过程”操作时,我收到消息:
Cannot call methods requiring a transaction if the entity manager has not been joined to the current transaction."
有服务代码:
package service;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
@WebService(serviceName = "HelloSPJPA")
public class HelloSPJPA {
@PersistenceUnit
EntityManagerFactory emf;
@Resource
UserTransaction utx;
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
@WebMethod(operationName = "callprocedure")
public String callProcedure(@WebParam(name = "name") String txt) {
String result = "OK";
Query query = emf.createEntityManager().createNativeQuery("begin p_test_insert(message_in => ?); end;").setParameter(1, txt);
try {
utx.begin();
query1.executeUpdate();
utx.commit();
} catch (Exception ex) {
Logger.getLogger(HelloSPJPA.class.getName()).log(Level.SEVERE, null, ex);
result = ex.getMessage();
}
return result;
}
}
我不知道如何在交易中加入该实体经理?有什么建议吗?
谢谢!