Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:54:36+00:00 2026-06-08T20:54:36+00:00

Note: Although at first similar, this is not a duplicate of Using Spring, JPA

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:54:38+00:00Added an answer on June 8, 2026 at 8:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that although it sounds similar, this is not the common how to
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
Note first of all that this question is not tagged winforms or wpf or
Note I am using Grails 2.0.4 and spring security core plugin 1.2.7.2 This appears
Note that this is for an ApiController in MVC 4 although I think it
(Note: This is not a question about what is the best way with code
Note: Not sure if this is the right stack, please tell if I should
Note: I had another similar question about how to GZIP data using Ruby's zlib
Note: this is with SQLite, although I expect the problem is on the Qt
Important note : this question is not about the superiority of a bracing style

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.