deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Kaltepoth <christ...@kaltepoth.de>
Subject Re: i write a simple DS jpa demo,still hava two bugs.
Date Sat, 28 Sep 2013 07:40:58 GMT
Regarding BUG2:

AFAIK you can remove the closeEntityManager() method because the lifecycle
of the EntityManager is managed by the container. I think our documentation
may need some improvement regarding this.

Christian



2013/9/28 εδΈ‰ιƒŽ <mihaisheng@163.com>

>   hi everyone:
> i write a simple jpa demo,still hava two bugs.
> (i use jboss as 7.1.1.final ; jdk1.7)
>
> 1,follow deltaspike jpamodule documents,i define a EntityManagerProducer
> public class EntityManagerProducer
> {
>     @PersistenceContext(unitName = "persistence0")
>     private EntityManager entityManager;
>     @Produces
>     @RequestScoped
>     protected EntityManager createEntityManager()
>     {
>         return this.entityManager;
>     }
>     protected void closeEntityManager(@Disposes EntityManager
> entityManager)
>     {
>         if (entityManager.isOpen())
>         {
>             entityManager.close();
>         }
>     }
> }
>
>
> 2,and i want do some database action:
> ---------------------------
> @Named("jpaTest")
> public class JpaTest
> {
>     @Inject
>     private EntityManager entityManager;
>     @Named
>     @Transactional
>     public void executeInTransaction(){
>         Query query = null;
>         String sql="insert into grandscape_user (user_name,user_password)
> values (:u,:p)";
>         query=entityManager.createNativeQuery(sql);
>         query.setParameter("u", "2013");
>         query.setParameter("p", "20132222");
>         query.executeUpdate();
>     }
> }
> -------------------
>
>
> 3,i use jsf  to call method #{jpaTest.executeInTransaction}.
> <h:commandButton value="jpa Method"
> action="#{jpaTest.executeInTransaction}" />
>
> 4,and persistence.xml.
> if transaction-type="JTA" ,the code run to [A JTA EntityManager cannot use
> getTransaction] error.
>
> (BUG1)
> i add the alternatives section to beans.xml to fix.
> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance"
>     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
>     <interceptors>
>
> <class>org.apache.deltaspike.security.impl.extension.SecurityInterceptor</class>
>
> <class>org.apache.deltaspike.jpa.impl.transaction.TransactionalInterceptor</class>
>     </interceptors>
>     <alternatives>
>
>  <class>org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy</class>
>     </alternatives>
> </beans>
> but the code still run to error(A JTA EntityManager cannot use
> getTransaction())
> so i set transaction-type="RESOURCE_LOCAL".
> ---------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
> http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
>     version="2.0">
>     <persistence-unit name="persistence0"
> transaction-type="RESOURCE_LOCAL">
>     <provider>org.hibernate.ejb.HibernatePersistence</provider>
>     <jta-data-source>java:jboss/datasources/MysqlDS</jta-data-source>
>         <properties>
>             <!-- Properties for Hibernate (default provider for JBoss AS)
> -->
>             <property name="hibernate.dialect"
> value="org.hibernate.dialect.MySQLDialect"/>
>                <property name="hibernate.connection.username" value="root"
> />
>                    <property name="hibernate.connection.password"
> value="root" />
>                    <property name="hibernate.connection.driver_class"
> value="com.mysql.jdbc.Driver" />
>                    <property name="hibernate.connection.url"
> value="jdbc:mysql://localhost:3306/grandscape_website" />
>
>             <property name="hibernate.show_sql" value="true"/>
>             <!-- Only format when you need to debug, because it slows
> things down -->
>             <property name="hibernate.format_sql" value="false"/>
>             <property name="hibernate.show_sql" value="true" />
>             <property name="hibernate.hbm2ddl.auto" value="update" />
>             <!--
>             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
>             -->
>         </properties>
>     </persistence-unit>
> </persistence>
> ------------
>
>
> 5,finally,when i called #{jpaTest.executeInTransaction} method,the data
> was fill to database successful,
> but the code run to a error:
>
> (BUG2)
> --------------------------
>
> ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host]]
> (http--0.0.0.0-8080-1) Exception sending request destroyed lifecycle event
> to listener instance of class org.jboss.weld.servlet.WeldListener:
> java.lang.IllegalStateException: JBAS011424: Container managed entity
> manager can only be closed by the container (auto-cleared at tx/invocation
> end and closed when owning component is closed.)
>     at
> org.jboss.as.jpa.container.TransactionScopedEntityManager.close(TransactionScopedEntityManager.java:98)
> [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
>     at
> persist.EntityManagerProducer.closeEntityManager(EntityManagerProducer.java:21)
> [classes:]
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> [rt.jar:1.7.0_07]
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> [rt.jar:1.7.0_07]
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> [rt.jar:1.7.0_07]
>     at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_07]
>     at
> org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:264)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:260)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:170)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.introspector.ForwardingWeldMethod.invokeOnInstance(ForwardingWeldMethod.java:51)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:154)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.bean.DisposalMethod.invokeDisposeMethod(DisposalMethod.java:158)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.bean.ProducerMethod$1.dispose(ProducerMethod.java:122)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at org.jboss.weld.bean.ProducerMethod.destroy(ProducerMethod.java:197)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.ForwardingContextual.destroy(ForwardingContextual.java:31)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.AbstractContext.destroy(AbstractContext.java:132)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.AbstractContext.destroy(AbstractContext.java:146)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.AbstractManagedContext.deactivate(AbstractManagedContext.java:41)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.AbstractBoundContext.deactivate(AbstractBoundContext.java:72)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.context.http.HttpRequestContextImpl.deactivate(HttpRequestContextImpl.java:86)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.jboss.weld.servlet.WeldListener.requestDestroyed(WeldListener.java:103)
> [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
>     at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
> [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_07]
> ---------------------------------
>
> if i delete [entityManager.close();] line in  EntityManagerProducer,the
> error disappeared.




-- 
Christian Kaltepoth
Blog: http://blog.kaltepoth.de/
Twitter: http://twitter.com/chkal
GitHub: https://github.com/chkal

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message