簡體   English   中英

無法查找 JNDI 名稱 [jdbc/jbpm-ds]

[英]Unable to lookup JNDI name [jdbc/jbpm-ds]

我正在嘗試開發 jBPM spring 引導應用程序,但出現無法查找 JNDI 名稱 [jdbc/jbpm-ds] 錯誤。

為此,我使用了 jbpm-7.36.0.Final、Spring-Boot-2.2.6 和 jbpm-spring-boot 7.36.0 Final 以及 spring 引導 Bitronix api。 下面是我的 pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jta-bitronix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <!-- <version>5.2.6.RELEASE</version> -->
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>jbpm-spring-boot-starter-basic</artifactId>
        <version>${runtime.version}</version>
    </dependency>
</dependencies>

在我的 application.properties 文件中,我配置了以下值

#hibernate configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update

#JTA enabled
spring.jta.enabled=true

#bitronix Non-XA transaction manager configuration
spring.datasource.xa.properties.url=jdbc:sqlserver://10.1.5.209:1433;databaseName=MRR3;
spring.datasource.xa.properties.uniqueName=jdbc/jbpm-ds
spring.datasource.xa.properties.username=sa
spring.datasource.xa.properties.password=password_123
spring.datasource.xa.properties.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.xa.data-source-class-name=bitronix.tm.resource.jdbc.lrc.LrcXADataSource

下面是代碼片段

    @Bean
    public StatefulKnowledgeSession newStatefulKnowledgeSession() throws Exception {
        RuntimeEnvironmentBuilder builder = null;
        if (usePersistence) {
            TransactionManager tm = TransactionManagerServices.getTransactionManager();

            entityManagerFactory = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");//Line where Exception occurs
            System.out.println(entityManagerFactory.getProperties());

            Environment env = EnvironmentFactory.newEnvironment();
            env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, entityManagerFactory);
            env.set(EnvironmentName.TRANSACTION_MANAGER, tm);

            builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
                    .entityManagerFactory(entityManagerFactory)
                    .addEnvironmentEntry(EnvironmentName.TRANSACTION_MANAGER, tm);

            builder.knowledgeBase(readKnowledgeBase);

            StatefulKnowledgeSession ksession = JPAKnowledgeService
                    .newStatefulKnowledgeSession(readKnowledgeBase, null, env);

            return ksession;
            // builder =
            // RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
            // .entityManagerFactory(entityManagerFactory);
        }
    }

分享重要的日志信息

2020-05-11 23:41:12.122  INFO 18852 --- [main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: org.jbpm.persistence.jpa
    ...] {} <>
2020-05-11 23:41:12.155  WARN 18852 --- [main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newStatefulKnowledgeSession' defined in class path resource [com/citiustech/mrr/controller/AbstractionProcessController.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.kie.internal.runtime.StatefulKnowledgeSession]: Factory method 'newStatefulKnowledgeSession' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] {} <>
.
.
.
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.citiustech.mrr.controller.AbstractionProcessController.newStatefulKnowledgeSession(AbstractionProcessController.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 20 common frames omitted
Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [jdbc/jbpm-ds]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:100)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:100)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
    ... 39 common frames omitted
Caused by: javax.naming.NameNotFoundException: unable to find a bound object at name 'jdbc/jbpm-ds'
    at bitronix.tm.jndi.BitronixContext.lookup(BitronixContext.java:83)
    at bitronix.tm.jndi.BitronixContext.lookup(BitronixContext.java:67)
    at javax.naming.InitialContext.lookup(InitialContext.java:421)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:97)
    ... 48 common frames omitted

我也有興趣知道我的以下理解是否正確

  1. jBPM 只支持JTA 類型的事務,即全局事務。 在集成 jBPM 時不能使用 RESOURCE_LOCAL 事務類型。
  2. 當我說事務類型 RESOURCE_LOCAL 時,意味着只有一個數據源事務將在應用程序中共享。
  3. jBPM RuntimeManager 使用 'org.jbpm.domain' 持久化單元名稱(強制類型)來處理它的所有內部表操作。
  4. 所有特定於應用程序的事務都可以通過默認的持久性單元名稱(即'org.jbpm.persistence.jpa')來處理。 這可以被自定義持久性上下文覆蓋。
  5. JTA進一步分為XADatasource類型和Non-XADatasource類型兩種。 當數據源位於應用服務器容器中時,應使用 XADatasource。 Non-XADatasource 對於基於 EJB 的應用程序很有用。
  6. 以下配置需要 XADatasource

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" /> <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform" />

  7. 配置下需要非 XADatasource

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>

  8. 第 6 點或第 7 點是否必要,如果需要,如何不使用 persistence.xml。

  9. 下面的屬性有什么用,jar有什么這個class

    spring.jpa.properties.hibernate.transaction.manager_lookup_class=org.hibernate.transaction.BTMTransactionManagerLookup

有一些誤解:

  • 正在使用jbpm-spring-boot-starter-basic ,但是您沒有使用任何 spring 加載的 bean 來創建 StatefulKnowledgeSession。 初學者的主要思想是避免低級框架配置。
  • 這個參數: spring.datasource.xa.properties.uniqueName沒有出現在文檔中,是駝峰式的。

方案一:Spring開機

使用正式版及其配置

#data source configuration
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.url=jdbc:h2:./target/spring-boot-jbpm;MVCC=true
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.XADataSource

#hibernate configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

#transaction manager configuration
spring.jta.narayana.transaction-manager-id=1

正如我們所見, jbpm-spring-boot-starter-basic依賴於spring-boot數據源引擎,沒有任何新的數據庫配置。 為什么? 因為StatefulKnowledgeSession只需要一個經典的數據庫連接。

也許這個啟動器初始化一個StatefulKnowledgeSession准備在任何 spring java bean 中使用@Autowire

方案二:Spring + 手動配置

一項簡單的研究表明,從JPAKnowledgeService創建StatefulKnowledgeSession需要經典:

EntityManagerFactory 和 TransactionManager

這也可以在您的代碼中看到:

Environment env = EnvironmentFactory.newEnvironment();
            env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, entityManagerFactory);
            env.set(EnvironmentName.TRANSACTION_MANAGER, tm);

在這個例子中:

http://www.javased.com/index.php?source_dir=droolsjbpm-integration/drools-container/drools-spring/src/main/java/org/drools/container/spring/beans/StatefulKnowledgeSessionBeanFactory.java

public static class JpaConfiguration {
  private EntityManagerFactory emf;
  private PlatformTransactionManager tm;
}
//..
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set( EnvironmentName.ENTITY_MANAGER_FACTORY,
jpaConfiguration.getEntityManagerFactory() );
env.set( EnvironmentName.TRANSACTION_MANAGER,
jpaConfiguration.getPlatformTransactionManager() );

所以解決方案可能只是配置一個 spring 引導來擁有優雅的自動連線:

//@SpringBootApplication or @Configuration
public class SpringMainApplication{
  @Autowire
  private EntityManagerFactory emf;
  @Autowire
  private PlatformTransactionManager tm;
  @Autowire
  private KnowledgeBase kbase;

  @Bean
  public KnowledgeBase readKnowledgeBase() throws Exception {
    List<RuleResource> resources = new ArrayList<RuleResource>();
    resources.add(new RuleResource("zzz/explore/ruleengine/rules/CalcPI.drl", ResourceType.DRL));
    return KnowledgeBaseHelper.readKnowledgeBase(resources);
  }

  @Bean
  public StatefulKnowledgeSession newStatefulKnowledgeSession() throws Exception {
    //if KnowledgeBase autowire does not works, just call directly
    //KnowledgeBase kbase =  readKnowledgeBase();
    Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set( EnvironmentName.TRANSACTION_MANAGER,tm);

    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    return ksession;
  }

}

現在,我們需要配置 spring 啟動此實例以供使用:

@Autowire
private EntityManagerFactory emf;
@Autowire
private PlatformTransactionManager tm;

我們可以這樣做:

//@SpringBootApplication or @Configuration
class SpringMainApplication {

  //spring boo twill auto-provide a Bean of type DataSource
  // if the properties are there
  @Autowired
    private DataSource dataSource;

  @Bean
  public EntityManagerFactory entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.acme.domain");
    factory.setDataSource(dataSource);
    factory.afterPropertiesSet();

    return factory.getObject();
  }

  @Bean
  public PlatformTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory());
    return txManager;
  }
}

有很多文檔和示例可以創建 EntityManagerFactory 和 TransactionManager。 檢查參考部分。

如果您適當地使用啟動器,也可能不需要此配置

解決方案 3:手動創建實例

如您所見,只需要一個entitymanager 和transactionmanager。 此外,要創建實體管理器,還需要有效的數據源。 所以你只需要這樣的東西:

//@SpringBootApplication or @Configuration
public class SpringMainApplication{

  @Bean
  public KnowledgeBase readKnowledgeBase() throws Exception {
    List<RuleResource> resources = new ArrayList<RuleResource>();
    resources.add(new RuleResource("zzz/explore/ruleengine/rules/CalcPI.drl", ResourceType.DRL));
    return KnowledgeBaseHelper.readKnowledgeBase(resources);
  }

  @Bean
  public StatefulKnowledgeSession newStatefulKnowledgeSession() throws Exception {

    //Crete datasource
    //https://gist.github.com/jrichardsz/8b8dffcf1cdf42444654abc227d9f4c1
    //or use the spring provided datasource

    //create an entity manger
    EntityManagerFactory emf = ...;
    //create an transaction manger
    PlatformTransactionManager tm =....;

    Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set( EnvironmentName.TRANSACTION_MANAGER,tm);

    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    return ksession;
  }

}

}

筆記

  • 這些片段沒有經過測試,因為流口水和數據庫不容易配置,並且您沒有共享最小的可運行示例。

  • 我將添加本研究的 http 鏈接

參考

正如@JRichardsz 所建議的那樣,我對代碼進行了以下更改,以使其與 Spring Boot 一起使用

@Autowired
private AbstractPlatformTransactionManager jtaPlatform;

@PersistenceUnit(name = "org.jbpm.persistence.jpa")
private EntityManagerFactory entityManagerFactory;

@Bean
public StatefulKnowledgeSession newStatefulKnowledgeSession() throws Exception {
    RuntimeEnvironmentBuilder builder = null;
    if (usePersistence) {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
                .entityManagerFactory(entityManagerFactory)
                .addEnvironmentEntry(EnvironmentName.TRANSACTION_MANAGER, jtaPlatform);

        builder.knowledgeBase(readKnowledgeBase);

        RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(builder.get());

        return (StatefulKnowledgeSession) manager.getRuntimeEngine(EmptyContext.get())
                .getKieSession();
    } 
}

對我來說 xa.properties 與 Bitronix 一起工作,但如果它不適合你,那么你可以切換到 Spring 提供的數據源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM