代码之家  ›  专栏  ›  技术社区  ›  adrian.riobo

glassfish 4上的jdbc/default

  •  2
  • adrian.riobo  · 技术社区  · 10 年前

    我正在将现有应用程序从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数据源元素指定一个值,或者将事务类型元素设置为RESOURCE_LOCAL,并为非jta数据来源元素指定值。

    此外,我还使用如下配置的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内核3.3.0.SP1

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

    关于如何解决这个问题有什么想法吗?

    1 回复  |  直到 10 年前
        1
  •  2
  •   adrian.riobo    10 年前

    我发现了问题,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/DBMmine。