SimpleNamingContextBuilder在使用spring的独立项目中不支持[javax.naming.Name]

时间:2017-12-11 08:16:24

标签: spring datasource jndi jta

我正在尝试使用带有jtaDataSource的Spring编写一个独立项目。要将数据源绑定到上下文,我在初始化applicationContext之前使用SimpleNamingContextBuilder。当我使用属性名称dataSource和entityManagerFactory bean时,我正在完美地工作,但是当我想要使用jtaDataSource它给了我那个错误" org.hibernate.engine.jndi.JndiException:解析JNDI名称[javax.transaction.TransactionManager]" &#时出错34; javax.naming.OperationNotSupportedException:SimpleNamingContext不支持[javax.naming.Name]"

例外情况:

Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [javax.transaction.TransactionManager]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:112)
    at org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform.locateTransactionManager(WeblogicJtaPlatform.java:41)
    at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.retrieveTransactionManager(AbstractJtaPlatform.java:104)
    at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.getTransactionManager(AbstractJtaPlatform.java:115)
    at org.hibernate.engine.transaction.jta.platform.internal.TransactionManagerBasedSynchronizationStrategy.canRegisterSynchronization(TransactionManagerBasedSynchronizationStrategy.java:56)
    at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.canRegisterSynchronization(AbstractJtaPlatform.java:148)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.attemptToRegisterJtaSync(TransactionCoordinatorImpl.java:252)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.pulse(TransactionCoordinatorImpl.java:289)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1584)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
    at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
    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.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:407)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:560)
    at com.sun.proxy.$Proxy54.createEntityManager(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:288)
    at com.sun.proxy.$Proxy101.getDelegate(Unknown Source)
    at org.springframework.data.jpa.provider.PersistenceProvider.fromEntityManager(PersistenceProvider.java:295)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.<init>(JpaRepositoryFactory.java:58)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.createRepositoryFactory(JpaRepositoryFactoryBean.java:79)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.doCreateRepositoryFactory(JpaRepositoryFactoryBean.java:69)
    at org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport.createRepositoryFactory(TransactionalRepositoryFactoryBeanSupport.java:72)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:227)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 25 more
Caused by: javax.naming.OperationNotSupportedException: SimpleNamingContext does not support [javax.naming.Name]
    at org.springframework.mock.jndi.SimpleNamingContext.getNameParser(SimpleNamingContext.java:287)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:505)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:135)
    ... 56 more

Java类定义如下:

   @SuppressWarnings("resource")
   public static void main(String[] args) {

      try {

         activateDataSourcesOnContext();

      } catch (IllegalStateException | NamingException e) {

         logger.error("Error on activateDataSourceOnContext {}", e.getMessage(), e);
      }

      new ClassPathXmlApplicationContext("applicationContext.xml");

      logger.debug("ApplicationContext has been up");
   }

   /*
    * Before Initializing ApplicationContext,we have to create mock context and
    * bind datasource to it due to not using a real container[Application server
    * etc.]
    */
   private static void activateDataSourcesOnContext() throws IllegalStateException, NamingException {

      SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
      builder.bind(CressScheduleProperties.CREWDB_CONN_JNDI_NAME, CressScheduleDBUtil.getDataSource());
      builder.activate();

   }

applicationContex定义如下:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="xJndi" />
    <property name="resourceRef" value="true" />
</bean>

<!-- ################################################################################################################ -->

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.x.jpa" />
    <property name="jtaDataSource" ref="dataSource" />
    <property name="validationMode" value="NONE" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="ORACLE" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>

            ....

            <prop key="hibernate.transaction.auto_close_session">true</prop>
            <prop key="hibernate.connection.release_mode">auto</prop>
            <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform
            </prop>

        </props>
    </property>
</bean>

<!-- ################################################################################################################ -->

<tx:jta-transaction-manager />


<jpa:repositories base-package="com.x.repo" />

0 个答案:

没有答案