0

I have problem when i try configure 2 dataSources in my xml applicationContext.xml. The examples i find reffer using bean annotation for configuration. i need configuration in xml in my actual architecture.

I saw tutorial:

http://www.baeldung.com/spring-data-jpa-multiple-databases

and

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources

But i don't solve my problem, the method used in this Spring page use annotation. I can't use annotation, my configuration is there in xml. When i try apply seconf datasource has error.

Before add second datasource, work's fine! When add second datasource don't work.

My applicationContext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                        http://www.springframework.org/schema/jee 
                        http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
                        http://www.springframework.org/schema/util 
                        http://www.springframework.org/schema/util/spring-util-4.2.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task-4.2.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/data/jpa 
                        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
    default-autowire="byName" default-lazy-init="true">

    <context:annotation-config />

    <context:component-scan base-package="br.com.myProject" />

    <jpa:repositories base-package="br.com.myProject.ged.repository"/>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        <property name="jndiName" value="java:jboss/datasources/sgedDS" />
        <property name="resourceRef" value="true" />    
    </bean>

    <bean id="dataSourceNurer" class="org.springframework.jndi.JndiObjectFactoryBean"  scope="singleton">
        <property name="jndiName" value="java:jboss/datasources/nurerDS" />
        <property name="resourceRef" value="true" />    
    </bean>


    <!--  ************** ENTITY MANAGER SGED ******************** -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="br.com.myProject.ged.entity" />
        <property name="dataSource" ref="dataSource" />  
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <!--  ************** ENTITY NURER NURER ******************** -->    

    <bean id="entityManagerFactoryNurer" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="br.com.myProject.ged.entity" />
        <property name="dataSource" ref="dataSourceNurer" /> 

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <!--  ******** SGED  ******** -->

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

    <!--  ******** NURER  ******** -->

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

    <tx:annotation-driven transaction-manager="transactionManager" /> <!-- SGED -->
    <tx:annotation-driven transaction-manager="transactionManagerNurer" /> <!-- NURER -->


    <bean id="persistenceExceptionTranslationPostProcessor" 
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="br.com.myProject.ged.spring.SpringViewScope" />
                </entry>
            </map>
        </property>
    </bean>

</beans>

My Bean Service layer:

@Transactional (transactionManager = "transactionManager2")
    public List<DataBase2Entity> getAll(){
        return nurerSituacaoIdrRepository.findAll();
        // return new ArrayList<DataBase2Entity>();
    }


@Transactional (transactionManager = "transactionManager")
    public List<DataBaseEntity> getAll(){
        return nurerSituacaoIdrRepository.findAll();
        // return new ArrayList<DataBaseEntity>();
    }

My BaseDao.java

public abstract class BaseDao<T>  {

    private Class<T> entityClass;

    @PersistenceContext(unitName = "entityManagerFactory")
    private EntityManager em;


    @PersistenceContext(unitName = "entityManagerFactoryNurer")
    private EntityManager emNurer;

    @SuppressWarnings("unchecked")
    public BaseDao() {
        this.entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

...

UPDATE : 02/10/2017 ERROR execution time:

Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values

I use same entityManagerFactory or create another entityManagerFactory (see BaseDao.java).

paulo.sobrinho
  • 83
  • 1
  • 1
  • 11

1 Answers1

0

This seems to be an issue with XML. I see there is a . (dot) at the end on line no #105

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

Remove the dot and try again.

Arun
  • 3,701
  • 5
  • 32
  • 43
  • Thank You. After ajust occur error: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values – paulo.sobrinho Oct 02 '17 at 13:47
  • Whe use same entityManager return error: could not extract ResultSet; SQL [n/a] – paulo.sobrinho Oct 02 '17 at 14:06
  • cause: org.hibernate.exception.SQLGrammarException: could not extract ResultSet – paulo.sobrinho Oct 02 '17 at 14:15
  • This is because something is not right the the SQL query. Since you are using HQL (Hibernate) verify that. – Arun Oct 02 '17 at 15:02
  • give the HQL query that you use. Mask the confidential data. – Arun Oct 02 '17 at 15:03
  • Im use not implementation methods. Im use: ObjectRepository.findAll() – paulo.sobrinho Oct 02 '17 at 16:31
  • In that case, the Entity objects are not properly mapped. Make sure the entity classes are matching the Table and entity class's package is specified in the hibernate configuration there by keeping it standard. – Arun Oct 02 '17 at 16:34
  • I think it is new EntityManager. Error creating bean with name 'transactionManagerNurer' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values – paulo.sobrinho Oct 02 '17 at 17:09
  • My configuration many similar this: http://www.codingpedia.org/ama/how-to-setup-multiple-data-sources-with-spring-and-jpa/ – paulo.sobrinho Oct 02 '17 at 17:12
  • I fix it contect.xml: Has elements in table, but returned this: org.hibernate.exception.SQLGrammarException: could not extract ResultSet – paulo.sobrinho Oct 02 '17 at 17:20
  • https://stackoverflow.com/questions/17152190/could-not-extract-resultset or https://stackoverflow.com/questions/20089031/could-not-extract-resultset-in-hibernate – Arun Oct 02 '17 at 17:30
  • i solved my problem. i m using jpql with jpa. i can't use Spring Data, i dont have time now. but i will searching this solution for future. Your answer helped me. Thank You Arun. – paulo.sobrinho Oct 03 '17 at 13:00