【问题标题】:jdbc/_default on glassfish 4glassfish 4 上的 jdbc/_default
【发布时间】:2014-04-07 04:56:16
【问题描述】:

我正在将现有应用程序从 glassfish 3.1.2 迁移到 glassfish 4

这是我的 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="miUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>jdbc/DBMine</non-jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.show_sql" value="${persistence.showSql}"/>
        <property name="hibernate.format_sql" value="${persistence.formatSql}"/>
        <property name="hibernate.use_sql_comments" value="${persistence.commentSql}"/>
        <property name="hibernate.connection.autocommit" value="false"/>
        <property name="hibernate.cache.use_query_cache" value="false"/>
        <property name="hibernate.cache.use_second_level_cache" value="false"/>
        <property name="hibernate.cache.use_structured_entries" value="false"/>
        <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
        <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
        <property name="hibernate.transaction.flush_before_completion" value="true"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.connection.SetBigStringTryClob" value="true"/>
        <property name="hibernate.jdbc.batch_size" value="0"/>
        <property name="hibernate.max_fetch_depth" value="3"/>
        <property name="hibernate.default_batch_fetch_size" value="16"/>
        <property name="hibernate.order_updates" value="true"/>
        <!--permite avanzar X numeros la secuencia--> 
<!--    <property name="hibernate.id.new_generator_mappings" value="true"/>
        <property name="hibernate.id.optimizer.pooled.prefer_lo" value="true" />-->
        <!--<property name="hibernate.id.increment_size" value="50"/>-->
        <!--            <property name="hibernate.id.optimizer" value="hilo"/>-->
        <!--Deshabilita la comprobacion de que namedquerys-->
        <!--<property name="hibernate.query.jpaql_strict_compliance" value="false"/>-->
        <property name="hibernate.query.startup_check" value="false"/>
        <!-- GLASSFISH SPECIFIC: The following property is necessary for
        deployment within Glassfish.  Note that each application
        server vendor has its own unique value. -->
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
    </properties>
</persistence-unit>

正如文档中所说的选择不同的数据源:

要使用非默认数据库,请为 jta-data-source 元素指定一个值,或者将 transaction-type 元素设置为 RESOURCE_LOCAL 并为 non-jta-data-source 元素指定一个值。

我还使用了这样配置的 Spring RoutingDataSource 策略:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"    
   xmlns:context="http://www.springframework.org/schema/context"    
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">



<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>        
    <property name="persistenceUnitName" value="miUnit"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="generateDdl" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
        </bean>
    </property>
    <property name="jpaDialect">
        <bean class="mi.app.core.jpa.hibernate.CustomHibernateJpaDialect">
            <property name="flushMode" value="MANUAL"/>
        </bean>
    </property> 
</bean>

<bean id="dataSource" class="es.xunta.formacion.sifo3.core.routing.RoutingDataSource">    
    <property name="targetDataSources">
        <map key-type="es.xunta.formacion.sifo3.core.routing.DataSourceType">
            <entry key="BD1" value-ref="bd1DataSource"/>
            <entry key="BD2" value-ref="bd2DataSource"/>
            <entry key="BD3" value-ref="bd3DataSource"/>
            <entry key="BD4" value-ref="bd4DataSource"/>
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="bd1DataSource"/>
</bean>

  <bean id="parentDataSource"
      class="org.springframework.jndi.JndiObjectFactoryBean"
      abstract="true">        
</bean>

<bean id="bd1DataSource" parent="parentDataSource">
    <property name="jndiName" value="java:comp/env/jdbc/DBMine"/>
</bean>    

<bean id="bd2DataSource" parent="parentDataSource">
    <property name="jndiName" value="java:comp/env/jdbc/DBMine2"/>
</bean>  

<bean id="bd3DataSource" parent="parentDataSource">
    <property name="jndiName" value="java:comp/env/jdbc/DBMine3"/>
</bean> 

<bean id="bd4DataSource" parent="parentDataSource">
    <property name="jndiName" value="java:comp/env/jdbc/DBMine4"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>          
</bean>



<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />    

关于版本的一些信息:

我正在使用: 春季 3.1.1-发布 由于之前对使用 JPA1 的限制,Hibernate-entitymanager 3.4.0.GA。 Hibernate-core 3.3.0.SP1

这是在 glassfish 3.1.2 上工作的,但现在的问题是,当我部署应用程序时,它总是使用 jdbc/_default 作为默认数据源。它不关心persistence.xml 和spring beans 配置文件上的数据源。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: hibernate jakarta-ee persistence datasource glassfish-4


    【解决方案1】:

    我发现了问题,Glassfish 4 中的 jndi 名称只需要相对名称:

    https://glassfish.java.net/docs/4.0/application-development-guide.pdf

    对于必须映射到全局 JNDI 名称的所有其他组件依赖项,默认是相对于 java:comp/env 的依赖项名称。 例如,在@Resource(name="jdbc/Foo") DataSource ds;注解,全局 JNDI 名称为 jdbc/Foo

    示例 jdbc/DBMine 而不是 java:comp/env/jdbc/DBMine。

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 2016-05-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多