我的项目中的交易有问题。
当我调用使用sql查询从数据库中删除行的方法时,我得到错误:
Warning: /class/HistoryList.xhtml @28,245 actionListener="#{planConfirmedController.delete(planController.historyDate,planController.selectedDate)}": javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
javax.el.ELException: /class/HistoryList.xhtml @28,245 actionListener="#{planConfirmedController.delete(planController.historyDate,planController.selectedDate)}": javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
......................
这是查询:
"DELETE c FROM Class c WHERE c.classname = :classname"
删除方法(从内存中写入,因为我现在无法访问原始代码)
@Transactional
public void delete(Date d1, Date d2)
{
...
Query q = ejbFacade.createNamedQuery("..") ;
q.executeUpdate() ;
}
...
我使用executeUpdate方法执行查询
我的glassfish-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<parametr-encoding default-charset="UTF-8"/>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_pup_rootPool" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.DataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> <property name="serverName" value="...."/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="pup"/>
<property name="User" value="username"/>
<property name="Password" value="password"/>
<property name="URL" value="jdbc:mysql://....:3306/pup?useUnicode=true&characterEncoding=UTF-8"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="pup" object-type="user" pool-name="mysql_pup_rootPool"/>
</resources>
和persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pupPU" transaction-type="JTA">
<jta-data-source>pup</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
使用eclipselink与数据库通信的应用程序。感谢您的帮助。
答案 0 :(得分:0)
查看调用@Transactional注释方法的代码会很有帮助。
确保在调用方法时,您正在使用框架创建的方法存根而不是直接使用方法。
换句话说,假设此方法是在某个EJB中编写的,那么您应该从使用注入的EJB实例的EJB客户端,而不是从EJB实例本身调用该方法。
答案 1 :(得分:0)
我忘了从我的xhtml页面粘贴代码 我用这种方式调用方法删除:
<p:commandButton id="deleteButton" icon="ui-icon-trash" value="#{bundle.Delete}" actionListener="#{planConfirmedController.delete(planController.historyDate,planController.selectedDate)}" update=":growl,:ClassListForm">
<p:confirm header="Usunięcie" message="Czy napewno chcesz usunąć wpis z historii dla aktualnie wybranych danych?" icon="ui-icon-alert" />
</p:commandButton>
planConfirmedController源代码的一部分:
@ManagedBean(name = "planConfirmedController")
@SessionScoped
public class PlanConfirmedController implements Serializable {
@EJB
private pup_dao.PlanConfirmedFacade planConfirmedFacade ;
我在删除方法的第一篇文章中的planConfirmedFacde被称为ejbFacade(错误地)
好吧我终于解决了这个问题。交易没有用,因为在控制器中我使用@ManagedBean而不是@Named注释。