2

I am migrating an existing application from glassfish 3.1.2 to glassfish 4

This is my 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>

Just as documentation said about pick up a different data source:

To use a non-default database,either specify a value for the jta-data-source element, or set the transaction-type element to RESOURCE_LOCAL and specify a value for the non-jta-data-source element.

Also I use a Spring RoutingDataSource strategy configured like this:

<?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" />    

Some info on versions:

I am using: Spring 3.1.1-RELEASE Hibernate-entitymanager 3.4.0.GA due to previous restrictions on using JPA1. Hibernate-core 3.3.0.SP1

This was working on glassfish 3.1.2 but now the problem is that when I deploy the application it always uses the jdbc/_default as default datasource. It dosent care about datasource on persistence.xml nor on spring beans configuration file.

any ideas on how to fix this?

adrian.riobo
  • 495
  • 5
  • 11

1 Answers1

2

I found the problem, jndi names in Glassfish 4 just need the relative name:

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

For all other component dependencies that must be mapped to global JNDI names, the default is the name of the dependency relative to java:comp/env. For example, in the @Resource(name="jdbc/Foo") DataSource ds; annotation, the global JNDI name is jdbc/Foo

Example jdbc/DBMine instead of java:comp/env/jdbc/DBMine.

adrian.riobo
  • 495
  • 5
  • 11