简体   繁体   中英

Spring-hibernate jpa and jboss - Saving objects to a second database

Note: Although at first similar, this is not a duplicate of Using Spring, JPA with Hibernate to access multiple databases/datasources configured in Jboss

Dear Stackoverflow,

I had a spring-jpa with hibernate application running on jboss-4.2.1.GA and using a single database.

I now have a second spring-hibernate project bundled up in the same ear file with the project described above but it needs to use a second database. This second hibernate/spring project is set up with the database.properties and hibernate.cfg.xml files.

The two databases details are stored on jboss oracle-ds.xml file:

<datasources>
    <local-tx-datasource>
        <jndi-name>DefaultDS</jndi-name>
         ...
     </local-tx-datasource>
     <local-tx-datasource>
        <jndi-name>SecondDS</jndi-name>
         ...
     </local-tx-datasource>
</datasources>

My question is, in the second project, with objects for the second database and not the first one, how can I call sessionFactory for the second database whose details are stored on the oracle-ds.xml instead of using database.properties files? I have seen an example calling

@Resource(mappedName = "java:SecondDS")
private DataSource secondDS;
...
java.sql.Connection conn = secondDS.getConnection();

If it is that easy to obtain a connection, that is only useful for prepared statements, how can I in get hold of the sessionFactory? Is there a similar approach?

All examples I have seen refer to database.properties and not the jboss ds.xml file.

Thanks in advance

There is several solution, depending on how you bind the data source to a presistance context, Spring way, JPA way or Hibernate way....

Spring

To link spring/hibernate application to JNDI data source, you will need to use JndiObjectFactoryBean

<bean id="serverDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/blah"/>
  <property name="proxyInterface" value="javax.sql.DataSource"></property>
</bean>

This way you have a spring bean representing the JNDI data source. You will need to create 2 of them (one for each of you data source). They you need to inject the data source in your spring defined SessionFactory. The same can be use if you use Spring managed JPA entity manager.

JPA

If you are using JPA (session factory is hibernate not jpa...) you can also defined a jndi data source name in the corresponding persistance.xml file.

<persistence-unit name="sample">
  <jta-data-source>java:/DefaultDS</jta-data-source>
  ...
</persistence-unit>

You need to use unitName parameter when injecting the entityManager:

    @PersistenceContext(unitName="sample")  

Hibernate

For hibernate.cfg.xml file you can specify the JNDI data source with this property

<property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>

The database.property should be removed to be sure the jndi data source is used.

These are only example, the final result will depend on how you made your plumbing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM