我想使用JPA从特定表中删除所有行。
我做了什么:
public class EmptyBomTables extends HttpServlet {
private static final long serialVersionUID = 1L;
@PersistenceContext(unitName = "xxx")
public EntityManager em;
@Resource
UserTransaction utx;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Query q1 = em.createQuery("DELETE FROM BomModule");
Query q2 = em.createQuery("DELETE FROM BomItem");
Query q3 = em.createQuery("DELETE FROM ItemModuleConnection");
Query q4 = em.createQuery("DELETE FROM ModuleConnection");
try {
utx.begin();
} catch (NotSupportedException | SystemException e) {
e.printStackTrace();
}
q1.executeUpdate();
q2.executeUpdate();
q3.executeUpdate();
q4.executeUpdate();
try {
utx.commit();
} catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException e) {
e.printStackTrace();
}
}
}
错误:
09:30:30,197 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SSIS2].[EmptyBomTables]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet EmptyBomTables threw exception: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.executeUpdate(QueryNonTxInvocationDetacher.java:80) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at com.sicap.ssis2.bom.EmptyBomTables.doGet(EmptyBomTables.java:50) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:397) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_09]
09:30:30,218 ERROR [org.jboss.as.txn] (http-localhost-127.0.0.1-8080-1) JBAS010152: APPLICATION ERROR: transaction still active in request with status 1
我做错了什么?
答案 0 :(得分:9)
这就是问题,一切都必须在同一个try / catch中:
try {
utx.begin();
Query q3 = em.createQuery("DELETE FROM ItemModuleConnection");
Query q4 = em.createQuery("DELETE FROM ModuleConnection");
Query q1 = em.createQuery("DELETE FROM BomModule");
Query q2 = em.createQuery("DELETE FROM BomItem");
q1.executeUpdate();
q2.executeUpdate();
q3.executeUpdate();
q4.executeUpdate();
utx.commit();
} catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
e.printStackTrace();
}