These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: multiple database switcing using hibernate
PostPosted: Fri Nov 19, 2004 12:57 pm 
Newbie

Joined: Thu Nov 18, 2004 4:40 am
Posts: 8
Hi

I am new to Hibernate. I am having a requirement, where in I need to switch between two different databases. I am using hibernate properties file and would like to know how to create multiple SessionFactories.

Thanks in Advance,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Build two SessionFactory objects.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:03 pm 
Newbie

Joined: Thu Nov 18, 2004 4:40 am
Posts: 8
Hi Christian,

Thanks for the reply.

Can I have the sample code for building two sessionfactories and propeties file. I need to switch during run time.

I am facing a problem which ever is properties file is loaded, then the same is getting used despite changing sessionfactory and properties file.

I will be glad if you help me out on this.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use hibernate.cfg.xml instead of properites and call "new Configuration().configure("/config_A.cfg.xml").buildSessionFactory()", etc.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:14 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Hi Cristian,

If the databases share the same schema and domain model is it necessary to create two session factories?

Would we not be able to pass in a connection to our openSession method to connect to the correct datasource.

Our application is highly transactional so 2nd level cache isn't an option anyways.

Do you see any caveats with this approach?

Roll


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:15 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Sorry, should've been "Hi Chrisitan" ... force of habit as my nephew is Crisitan. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Never tried it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:32 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
I have a trivial test case that uses a single session factory with two jndi data source connections.

Code:
...
    public void testReasoningDatasource() throws Exception {
        JNDIDataSourceHelper.init(JNDI_UNIT_TEST_HELPER_PROPERTIES);
        InitialContext ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup(JNDIDataSourceHelper
                .getJndiName2());
        Connection connection = dataSource.getConnection();
        Session session = getSessionFactory().openSession(connection);
        Case aCase = (Case) session.load(Case.class,
                CASE_ID);
        String title = (String) aCase.getTitles().get("en");
        connection.close();
        assertTrue(title.equals("TEST_CASE_1"));
    }
   
    public void testJcdtEmptyDatasource() throws Exception {
        JNDIDataSourceHelper.init(JNDI_UNIT_TEST_HELPER_PROPERTIES);
        InitialContext ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup(JNDIDataSourceHelper
                .getJndiName1());
        Connection connection = dataSource.getConnection();
        Session session = getSessionFactory().openSession(connection);
        Case aCase = (Case) session.load(Case.class,
                CASE_ID);
        String title = (String) aCase.getTitles().get("en");
        connection.close();
        assertTrue(title.equals("TEST_CASE_2"));
    }
...


The only issues I see with this approach thus far are:

1) Cannot use with 2nd level cache, which is acceptable in highly transactional applications in the first place (however, I think cache regions might assist here(?))

2) A single transaction working with multiple datasources might introduced unexpected results. Particularly if a you are accessing the same entity from N datasources with the same ID. This is because of the transactional 1st level cache, and rightfully so. I don't have any use cases at present where this would be a problem. At any rate, I have test cases for this as well.

Any thoughts?

Roll


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 3:36 pm 
Newbie

Joined: Thu Nov 18, 2004 4:40 am
Posts: 8
Hi Christin,

I tried with the cfg.xml file.

The following is my code:

cfg.xml file 1)
<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="java:comp/env/hibernate/SessionFactory">
<!-- properties -->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.1.143/test</property>
<property name="show_sql">true</property>
</session-factory>

</hibernate-configuration>

cfg.xml file 2)
<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="java:comp/env/hibernate/SessionFactory">
<!-- properties -->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.1.137/storefront</property>
<property name="connection.username">storefront</property>
<property name="connection.password">storefront</property>
<property name="show_sql">true</property>
</session-factory>

The following is the code in which I am trying to dynamically change the schema:

if(dbType==1if(dbType==1){
Configuration configuration = new Configuration();
configuration.addClass(nl.tfe.ddb.business.entities.ActivePollHistory.class);
configuration.addClass(nl.tfe.ddb.business.entities.Banner.class);


sessionFactory1 = configuration.configure("/test.cfg.xml").buildSessionFactory();
session = sessionFactory1.openSession();
threadSession.set(session);
}
else{
Configuration configuration = new Configuration();
configuration.addClass(nl.tfe.ddb.business.entities.ActivePollHistory.class);
configuration.addClass(nl.tfe.ddb.business.entities.Banner.class);

sessionFactory2 = configuration.configure("/hibernate.cfg.xml").buildSessionFactory();
session = sessionFactory2.openSession();
threadSession.set(session)
}
//sessionFactory = configuration.buildSessionFactory();
return session;

#######################

The schema is loaded every time the same which I opted to load. I am unable to change the schema dynamically.

Please help me, I am breaking my head fo r the past two day.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you have the two session factories running at the same time you should also use two different JNDI names...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 3:53 pm 
Newbie

Joined: Thu Nov 18, 2004 4:40 am
Posts: 8
If I use only one session factory, then will it work. Also I am not using any JNDI mappings. I am simply calling the cfg.xm file in my configuration.

Will it be suffice?

Thanks for the quick reply.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 3:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Ok - if you have TWO different SessionFactories then it must work - you will have two SessonFactories that opens session with connections to whatever connection properties you have given them.

If you provide your own connection you can control where a session connects - but it's dangerous if you use any kind of 2nd level cache since it will probably not be aligned with both db's.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:04 pm 
Newbie

Joined: Thu Nov 18, 2004 4:40 am
Posts: 8
Hi Max,

Thanks for the reply. I tried with single sessionfactory and two also. But still I am getting the results of the one which opened first.

I will be glad if you can give me a sample code for dynamic databases.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:08 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Hi Gopi,

Max or Cristian can confirm, however, I believe if you are opening both sessions within the same transaction and trying to access mutually exclusive entities in from both datasources who posess the same id then you will run into this problem because the 1st level cache will assume that the first entity loaded is equal to that of the second entity.

Max or Cristian: I know this is dangerous, however, in a highly tx application with lots of writes where read-write cache provides little benefit then would you agree that a single session factory with passed different connections is acceptable?

Thx,
Roll[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:10 pm 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
Just wanted to add Gopi that if you are using mutliple datasources with a single SessionFactory use separate transactions for each datasource.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.