2017-02-15 275 views
0

我一直試圖讓JPAKnowledgeService現在有3天左右的時間,而且我已經接近放棄了,似乎太多的配置和細節對於它是/不。然而,不能讓Bitronix在Spring Boot項目中管理我的數據源/事務

this problem最初,這是走了,我添加

後到我jndi.properties文件,作爲答案提示。我最終創建了StatefulKnowledgeSession,並認爲工作已經結束。但是在drools聊天中,同一個人建議我的事務可能由Hibernate而不是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

,並按照these instructions給了一個JNDI名稱到我的數據源嵌入式的Tomcat。

和錯誤回來:

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看起來爲空。我究竟做錯了什麼?我感到沮喪和無知。

+0

@Akshay這是個玩笑嗎? –

+0

我剛纔看到了如此評論的錯誤.. – Akshay

+0

您是否意識到這不是一個NPE問題,而是一個關於如何配置Hibernate以使用Bitronix的問題?您將我的問題標記爲與我無關(並且與我的關係無關)並阻止潛在興趣的問題的重複。這是粗魯的,不理解。 –

回答

2

很顯然,我沒有註冊我的默認數據源作爲JNDI和Tomcat,但要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(); 
} 

一切否則保持不變,它的工作原理!

相關問題