Wildfly 8.2和Hibernate:无法注册同步

时间:2014-11-23 16:45:11

标签: hibernate wildfly wildfly-8

我正在尝试在WIldFly 8.2中提供restfull服务。我想使用hibernate来使用db。

1)我在wildfly的配置文件中创建了数据源。

2)我检查了管理控制台中的连接。它有效。

3)persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?>  
    <persistence version="2.0"  
        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">  
        <persistence-unit name="HSDB" transaction-type="JTA">  
            <jta-data-source>java:jboss/datasources/PostgreDataSource</jta-data-source>  
            <properties>  
                <property name="hibernate.hbm2ddl.auto" value="update" />  

                <property name="hibernate.show_sql" value="true" />  
                  <property name="format_sql" value="true" />  

            </properties>  
        </persistence-unit>  
    </persistence>  

User.java

  @Entity  
    @Table(name = "User", schema="public" )  
    public class User {  
        private int mId;  
        private String mText;  

        /** 
         * @return the pid 
         */  
        @Id  
        @GeneratedValue  
        @Column(name = "id")  
        public int getId() {  
            return mId;  
        }  

        /** 
         * @param pid 
         *            the pid to set 
         */  
        public void setText(String text) {  
            this.mText = text;  
        }  

        /** 
         * @return the pid 
         */  
        @Column(name = "text")  
        public String getText() {  
            return mText;  
        }  

        /** 
         * @param pid 
         *            the pid to set 
         */  
        public void setId(int pid) {  
            this.mId = pid;  
        }  

        @Override  
        public String toString() {  
            return String.format(  
                    "id=%d\nid =%s",  
                    this.mId);  
        }  

    }  

rs file:

  @Path("/")  
    public class HelloWorld {  

    @PersistenceUnit(unitName = "HSDB")  
        private EntityManagerFactory mEntityManagerFactory;  
    ...  

       @GET  
        @Path("/")  
        @Produces(MediaType.APPLICATION_XML)  
        @Consumes(MediaType.APPLICATION_XML)  
        public String getHelloWorldXML() {  
    //        EntityManagerFactory emf = Persistence.createEntityManagerFactory("HSDB"); //returns null  
    //        EntityManager em = emf.createEntityManager(); //null pointer exception on this line. emf is null  
            EntityManager em = mEntityManagerFactory.createEntityManager();  
            String id = "_def_";  
            Session session = (Session) em.getDelegate();  
            Transaction tx = null;  
            User person = null;  

            try {  
                tx = session.beginTransaction();  //<-- problem is here  
                person = (User) session.get(User.class, 7);  
                id = String.valueOf(person.getId());  
            } catch (HibernateException e) {  
                if (tx != null)  
                    tx.rollback();  
                e.printStackTrace();  
            } finally {  
                tx.commit();  
                session.close();  
            }  
            return "<xml><result>" + helloService.createHelloMessage("World")  + id + "</result></xml>";  


        }  
    }  

当我将项目部署到wildfly时,我在控制台hibernate日志中看到了:

19:39:57,618 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 147) HHH000204: Processing PersistenceUnitInfo [  
        name: HSDB  
        ...]  
    19:39:57,623 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016002: Processing weld deployment heartstone-server.war  
    19:39:57,640 INFO  [org.jboss.weld.deployer] (MSC service thread 1-13) JBAS016005: Starting Services for CDI deployment: heartstone-server.war  
    19:39:57,642 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016008: Starting weld service for deployment heartstone-server.war  
    19:39:57,645 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 147) JBAS011409: Starting Persistence Unit (phase 2 of 2) Service 'heartstone-server.war#HSDB'  
    19:39:57,649 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 147) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect  
    19:39:57,650 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (ServerService Thread Pool -- 147) HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4  
    19:39:57,651 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 147) HHH000397: Using ASTQueryTranslatorFactory  
    19:39:57,657 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 147) HHH000228: Running hbm2ddl schema update  
    19:39:57,658 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 147) HHH000102: Fetching database metadata  
    19:39:57,659 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 147) HHH000396: Updating schema  
    19:39:57,673 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 147) HHH000261: Table found: public.user  
    19:39:57,673 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 147) HHH000037: Columns: [id, text]  
    19:39:57,673 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 147) HHH000108: Foreign keys: []  
    19:39:57,673 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 147) HHH000126: Indexes: [user_pkey]  
    19:39:57,674 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 147) HHH000232: Schema update complete  

所以与数据库的连接很好。但是,当我尝试在代码中使用它时,我得到了这个错误:

  19:43:38,472 ERROR [stderr] (default task-35) org.hibernate.TransactionException: Could not register synchronization for container transaction  

    19:43:38,472 ERROR [stderr] (default task-35)     at org.hibernate.engine.transaction.internal.jta.CMTTransaction.afterTransactionBegin(CMTTransaction.java:72)  

    19:43:38,472 ERROR [stderr] (default task-35)     at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:166)  

    19:43:38,473 ERROR [stderr] (default task-35)     at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)  

    19:43:38,473 ERROR [stderr] (default task-35)     at ru.suvitruf.hs.server.HelloWorld.getHelloWorldXML(HelloWorld.java:86)  

    19:43:38,473 ERROR [stderr] (default task-35)     at ru.suvitruf.hs.server.HelloWorld$Proxy$_$$_WeldClientProxy.getHelloWorldXML(Unknown Source)  

    19:43:38,473 ERROR [stderr] (default task-35)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  

    19:43:38,473 ERROR [stderr] (default task-35)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  

    19:43:38,473 ERROR [stderr] (default task-35)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  

    19:43:38,473 ERROR [stderr] (default task-35)     at java.lang.reflect.Method.invoke(Unknown Source)  

    19:43:38,473 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)  

    19:43:38,473 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)  

    19:43:38,474 ERROR [stderr] (default task-35)     at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)  

    19:43:38,474 ERROR [stderr] (default task-35)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)  

    19:43:38,474 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)  

    19:43:38,474 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)  

    19:43:38,475 ERROR [stderr] (default task-35)     at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)  

    19:43:38,475 ERROR [stderr] (default task-35)     at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)  

    19:43:38,476 ERROR [stderr] (default task-35)     at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)  

    19:43:38,476 ERROR [stderr] (default task-35)     at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166)  

    19:43:38,477 ERROR [stderr] (default task-35)     at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)  

    19:43:38,477 ERROR [stderr] (default task-35)     at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759)  

    19:43:38,477 ERROR [stderr] (default task-35)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  

    19:43:38,477 ERROR [stderr] (default task-35)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  

    19:43:38,477 ERROR [stderr] (default task-35)     at java.lang.Thread.run(Unknown Source)  

1 个答案:

答案 0 :(得分:0)

将此属性添加到persistence.xml之后,它运行良好:

<property name="hibernate.transaction.factory_class"
        value="org.hibernate.transaction.JDBCTransactionFactory" />