使用JTA进行Hibernate会话处理(无法找到当前的JTA事务)

时间:2016-12-07 08:53:03

标签: java hibernate jboss wildfly jta

我使用Hibernate 4.3.7在Wildfly 8.2(JBoss)上运行java应用程序。我使用sessionFactory.openSession()与数据库进行交互。我想使用JTA更改为每个请求的会话模式。我是这个领域的新手。

到目前为止我做了什么:

1 - 我将数据源配置从hibernate.cfg.xml移动到Wildfly配置

<datasources>
    <datasource jta="true" jndi-name="java:jboss/datasources/Dummy" pool-name="Dummy" enabled="true"
                use-ccm="true">
        <connection-url>jdbc:mysql://localhost/dummy</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver>dummy-1.0.war_com.mysql.jdbc.Driver_5_1</driver>
        <security>
            <user-name>root</user-name>
            <password>root</password>
        </security>
        <validation>
            <validate-on-match>false</validate-on-match>
            <background-validation>false</background-validation>
        </validation>
        <timeout>
            <set-tx-query-timeout>false</set-tx-query-timeout>
            <blocking-timeout-millis>0</blocking-timeout-millis>
            <idle-timeout-minutes>0</idle-timeout-minutes>
            <query-timeout>0</query-timeout>
            <use-try-lock>0</use-try-lock>
            <allocation-retry>0</allocation-retry>
            <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
        </timeout>
        <statement>
            <share-prepared-statements>false</share-prepared-statements>
        </statement>
    </datasource>
    <drivers>
        <driver name="h2" module="com.h2database.h2">
            <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
        </driver>
    </drivers>
</datasources>

2 - 我将数据源,事务工厂,当前会话上下文类和JTA平台添加到hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.datasource">java:jboss/datasources/Dummy</property>

         <!--How to find the Transaction-->
        <property name="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</property>

        <!-- How to produce transaction -->
        <property name="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform</property>

        <!-- Session context with JTA -->
        <property name="hibernate.current_session_context_class">jta</property>

    </session-factory>
</hibernate-configuration>

3 - 我将jbossjta添加到我的gradle依赖项中

compile group: 'org.jboss.jbossts', name: 'jbossjta', version: '4.16.6.Final'

4 - 我从openSession改为getCurrentSession()

public class DatabaseManager {

    private static SessionFactory sessionFactory;

    private static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            sessionFactory = createSessionFactory();
        }

        return sessionFactory;
    }

    public static Session createSession() {
        SessionFactory sessionFactory = DatabaseManager.getSessionFactory();

        // Session session = sessionFactory.openSession();
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();

        return session;
    }

    public static void commitAndCloseSession(Session session) {
        session.getTransaction().commit();
        session.close();
    }

    private static SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration()
                .configure("hibernate-entities.cfg.xml");

        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate-connection.cfg.xml")
                .build();

        return configuration.buildSessionFactory(registry);
    }

}

当应用程序调用getCurrentSession时,抛出以下异常:

org.hibernate.HibernateException: Unable to locate current JTA transaction
09:26:31,199 INFO  [stdout] (default task-12)   at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:92) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
09:26:31,199 INFO  [stdout] (default task-12)   at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
09:26:31,199 INFO  [stdout] (default task-12)   at ch.dummy.database.DatabaseManager.createSession(DatabaseManager.java:36) ~[classes:?]
09:26:31,199 INFO  [stdout] (default task-12)   at ch.dummy.feature.authentication.requestfilter.BenutzerFilter.loadBenutzerInformation(BenutzerFilter.java:48) ~[classes:?]
09:26:31,199 INFO  [stdout] (default task-12)   at ch.dummy.feature.authentication.requestfilter.BenutzerFilter.loadBenutzerInformation(BenutzerFilter.java:37) ~[classes:?]
09:26:31,199 INFO  [stdout] (default task-12)   at ch.dummy.feature.authentication.requestfilter.BenutzerFilter.filter(BenutzerFilter.java:28) ~[classes:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:264) ~[resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) ~[resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) ~[resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.10.Final.jar!/:?]
09:26:31,199 INFO  [stdout] (default task-12)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar!/:1.0.0.Final]
09:26:31,199 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,199 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,200 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,200 INFO  [stdout] (default task-12)   at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) [wildfly-undertow-8.2.0.Final.jar!/:8.2.0.Final]
09:26:31,200 INFO  [stdout] (default task-12)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,200 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) [wildfly-undertow-8.2.0.Final.jar!/:8.2.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar!/:1.1.0.Final]
09:26:31,201 INFO  [stdout] (default task-12)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_71]
09:26:31,202 INFO  [stdout] (default task-12)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_71]

我错过了什么?到目前为止我做了正确的事吗?

0 个答案:

没有答案