-->
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.  [ 7 posts ] 
Author Message
 Post subject: Multiple session in hibernate?
PostPosted: Tue Jun 17, 2008 2:04 am 
Beginner
Beginner

Joined: Tue Jun 17, 2008 12:14 am
Posts: 21
If I have a hibernate apps that need to open multiple connection, how to
do this?


I think should be :
hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="connection.url">jdbc:sqlserver://PC1:XXXX;database=DB1</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">P@ssw0rd</property>
        <!-- Set AutoCommit to true -->
        <property name="connection.autocommit">true</property>
        <!-- SQL Dialect to use. Dialects are database specific -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <!-- Mapping files -->
      </session-factory>
   <session-factory>
        <property name="connection.url">jdbc:sqlserver://PC2:3751;database=DB2</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">P@ssw0rd</property>
        <!-- Set AutoCommit to true -->
        <property name="connection.autocommit">true</property>
        <!-- SQL Dialect to use. Dialects are database specific -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <!-- Mapping files -->
      </session-factory>
    </hibernate-configuration>


But what about java/POJO ?
this is for single connection:

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();

what about multiple?


Top
 Profile  
 
 Post subject: Multiple connection
PostPosted: Tue Jun 17, 2008 2:17 am 
Newbie

Joined: Wed Apr 30, 2008 4:46 am
Posts: 8
For multiple connections you can also create multiple configurations
For example..
Code:
Configuration cfg1 = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9iDialect");

Code:
Configuration cfg2 = new Configuration()
.addClass(org.hibernate.auction.Vendor.class)
.addClass(org.hibernate.auction.Item.class);


and then create SessionFactory
Code:
SessionFactory sessionFactory1 = cfg1.buildSessionFactory();
SessionFactory sessionFactory2 = cfg2.buildSessionFactory();

_________________
thanks,
windy

Well begun is half done.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 2:17 am 
Newbie

Joined: Wed Apr 30, 2008 4:46 am
Posts: 8
You can use the same POJOs.

_________________
thanks,
windy

Well begun is half done.


Top
 Profile  
 
 Post subject: Re: Multiple connection
PostPosted: Tue Jun 17, 2008 3:25 am 
Beginner
Beginner

Joined: Tue Jun 17, 2008 12:14 am
Posts: 21
Thanks.

But do you know how to do this in JBoss?
I thought it was:

Code:
SessionFactory sessionFactory = (SessionFactory)
ctx.lookup("java:/hibernate/SessionFactory");         
session = sessionFactory.openSession();

SessionFactory sessionFactory2 = (SessionFactory)
ctx.lookup("java:/hibernate/SessionFactory2");         
session2 = sessionFactory2.openSession();


And my "jboss-service.xml":

Code:
<?xml version="1.0" encoding="UTF-8"?>
<server>
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.adminguide:name=SessionFactory">
        <attribute name="DatasourceName">java:/DS</attribute>
        <attribute name="Dialect">org.hibernate.dialect.SQLServerDialect</attribute>
        <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
   <attribute name="QueryCacheEnabled" >true</attribute>
    </mbean>
   <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.adminguide:name=SessionFactory2">
        <attribute name="DatasourceName">java:/DS2</attribute>
        <attribute name="Dialect">org.hibernate.dialect.SQLServerDialect</attribute>
        <attribute name="SessionFactoryName">java:/hibernate/SessionFactory2</attribute>
      <attribute name="QueryCacheEnabled" >true</attribute>
    </mbean>
</server>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 4:01 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Connecting to multiple database should be easily accomplished by using the suggesting above. However, remember that your object model won't be able to span across those multiple databases. To have an object model that spans across multiple databases, you might consider Hibernate Shards.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 11:31 pm 
Beginner
Beginner

Joined: Tue Jun 17, 2008 12:14 am
Posts: 21
Cameron McKenzie wrote:
Connecting to multiple database should be easily accomplished by using the suggesting above. However, remember that your object model won't be able to span across those multiple databases. To have an object model that spans across multiple databases, you might consider Hibernate Shards.

Are you talking something about this:


Code:
public SessionFactory createSessionFactory() {
   Configuration prototypeConfig = new Configuration().configure("shard0.hibernate.cfg.xml");
   prototypeConfig.addResource("weather.hbm.xml");
   List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>();
   shardConfigs.add(buildShardConfig("shard0.hibernate.cfg.xml"));
   shardConfigs.add(buildShardConfig("shard1.hibernate.cfg.xml"));
   shardConfigs.add(buildShardConfig("shard2.hibernate.cfg.xml"));
   ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
   ShardedConfiguration shardedConfig = new ShardedConfiguration(
      prototypeConfig,
      shardConfigs,
      shardStrategyFactory);
   return shardedConfig.buildShardedSessionFactory();
   }


at http://www.hibernate.org/hib_docs/shards/reference/en/html_single/ ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:43 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
That's exactly what I'm talking about. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

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.