我一直在尝试制作JPAKnowledgeService
大约3天,而我几乎要放弃,这似乎太多的配置和细节工作是/所做的。但是,
我最初遇到了这个问题,它添加后消失了
java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
正如答案所暗示的那样,进入我的jndi.properties
文件。我最终能够创建一个StatefulKnowledgeSession
,并认为工作已经结束。但是在流口水聊天中,同一个人建议我的交易可能是通过冬眠而不是bitronix来处理的,这可能会使我的持久性完全非交易。
,我想他是正确的,因为每当我尝试将Object
插入知识会议并致电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
并通过按照以下说明给我的嵌入式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();
}
其他一切都保持不变,并且起作用!