java - 无法让 Bitronix 管理 Spring Boot 项目中的数据源/事务

标签 java hibernate jpa drools bitronix

我一直在尝试制作JPAKnowledgeService大约三天了,我几乎要放弃了,对于它的用途来说,它似乎需要太多的配置和细节工作。然而,

我有this problem最初,在我添加后就消失了

java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory

进入我的jndi.properties文件,正如答案所暗示的那样。我能够创建一个 StatefulKnowledgeSession终于,以为工作结束了。但在流口水聊天中,同一个人暗示我的事务可能是由 Hibernate 而不是 Bitronix 处理的,这可能会使我的持久性完全非事务性。

我想他是对的,因为每当我尝试插入Object时进入知识 session 并调用 fireAllRules ,我被困在:

executing transaction with 0 enlisted resource

后跟:

transaction timed out: a Bitronix Transaction with GTRID [3132372E302E312E310000000000AFB9D800000006], status=MARKED_ROLLBACK, 0 resource(s) enlisted (started Thu Jan 01 05:11:56 EET 1970)

之后我改变的是;我更新了我的persistence.xml如下:

    <?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_1_0.xsd" version="1.0">
    <persistence-unit  name="org.jbpm.persistence.jpa" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:comp/env/jdbc/jbpm</jta-data-source>
        <class>org.drools.persistence.info.SessionInfo</class>
        <properties>
            <property name="hibernate.jndi.class" value="bitronix.tm.jndi.BitronixInitialContextFactory"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
        </properties>
    </persistence-unit>
</persistence>

将此行添加到我的 application.properties 中:

spring.datasource.jndi-name=java:comp/env/jdbc/jbpm

并按照 these instructions 为我的嵌入式 tomcat 数据源指定了 jndi 名称.

错误又回来了:

Caused by: java.lang.NullPointerException: null
    at org.drools.persistence.jta.JtaTransactionManager.getStatus(JtaTransactionManager.java:273) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
    at org.drools.persistence.jpa.AbstractPersistenceContextManager.getApplicationScopedEntityManager(AbstractPersistenceContextManager.java:78) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
    at org.drools.persistence.jpa.JpaPersistenceContextManager.getApplicationScopedPersistenceContext(JpaPersistenceContextManager.java:55) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
    at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:103) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
    ... 43 common frames omitted

JPAKnowledgeService 相关的表格是在数据库中创建的,所以我猜我的 JNDI 注册是成功的,但我似乎无法找到 Bitronix 作为我的事务管理器,因为 JtaTransactionManager似乎为空。我究竟做错了什么?我感到沮丧和无能为力。

最佳答案

显然,我不必使用 Tomcat 将默认数据源注册为 JNDI,而是让 Bitronix 直接管理它,如下所示:

@Bean
public PoolingDataSource setupPoolingDataSource() {
    PoolingDataSource pds = new PoolingDataSource();
    pds.setUniqueName("jdbc/jbpm");
    pds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
    pds.setMaxPoolSize(50);
    pds.setAllowLocalTransactions(true);
    pds.getDriverProperties().put("user", "username");
    pds.getDriverProperties().put("password", "password");
    pds.getDriverProperties().put("url", "jdbc:mysql://localhost/databaseName?useUnicode=yes&characterEncoding=UTF-8&useSSL=false");
    pds.getDriverProperties().put("driverClassName", "com.mysql.jdbc.Driver");
    pds.init();
    return pds;
}

并删除了这些内容:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {

            ContextResource resource = new ContextResource();
            resource.setName(name);
            resource.setType(DataSource.class.getName());
            resource.setProperty("url", "...");
            resource.setProperty("username", "...");
            resource.setProperty("password", "...");
            resource.setProperty("driverClassName", "...");
            resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
            context.getNamingResources().addResource(resource);
        }
    };
}

@Bean
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {

    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setJndiName("...");
    bean.setProxyInterface(DataSource.class);
    bean.setLookupOnStartup(false);
    bean.afterPropertiesSet();
    return (DataSource)bean.getObject();
}

其他一切都保持不变并且可以正常工作!

关于java - 无法让 Bitronix 管理 Spring Boot 项目中的数据源/事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246901/

相关文章:

java - Avro 使用 json 转换生成类问题 [kotlin]

java - 结构设计模式

hibernate - org.h2.jdbc.JdbcSQL异常 : Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-193]

spring - 如何在 JPA 中明确声明一个实体是新的( transient 的)?

java - 线程中的异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range: 1

java - Java Salesforce 应用程序中的 "Could not find the main class"

java - Hibernate - 如何映射 EnumSet

java - Hibernate 多个原生 SQL 语句

sql - 性能 createNamedNativeQuerie 与 createNativeQuery

java - hibernate JPA : 'IS EMPTY' condition doesn't work for collections passed as parameters