-->
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.  [ 9 posts ] 
Author Message
 Post subject: Two Phase Commit
PostPosted: Mon Dec 08, 2003 10:47 am 
Newbie

Joined: Thu Nov 20, 2003 5:39 pm
Posts: 4
Location: Rio de Janeiro, Brazil
Hello,

I'm using Hibernate 2 and using SessionFactory to open a connection to the database.

Can I connect to two databases within one web application? And can you show me how to do it?

Two phase commit !!!! Does exist in hibernate ?????

Thanks

Carlos Magno

_________________
Carlos Magno
Java Developer - N


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 11:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Open 2 session factories with 2 different Configuration

2-PC exists in Hibernate if your transaction manager manage it. hibernate always delegate Transaction to etiher:
- JDBC
- JTA

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 8:14 am 
Newbie

Joined: Thu Nov 20, 2003 5:39 pm
Posts: 4
Location: Rio de Janeiro, Brazil
epbernard wrote:
Open 2 session factories with 2 different Configuration

2-PC exists in Hibernate if your transaction manager manage it. hibernate always delegate Transaction to etiher:
- JDBC
- JTA


Yeah, I have used 2 session factory and 2 different configuration. Look:

Code:
public void configureTwoPhaseCommit() throws HibernateException {
    Configuration cfg = new Configuration();
    Configuration cfg2 = new Configuration();

    cfg.configure("/hibernate.cfg.xml");
    cfg2.configure("/hibernatebkp.cfg.xml");
      
    sessionFactory = cfg.buildSessionFactory();
    sessionFactory2 = cfg2.buildSessionFactory();
}


So, I use them here:
Code:
   public Blog createBlog(String name) throws HibernateException{
       
      Blog blog = new Blog();
      blog.setName(name);
      blog.setItems( new ArrayList() );
       
      Session session = sessionFactory.openSession();
      Session sessionBackup = sessionFactory2.openSession();
      
      Transaction tx = null;
      Transaction tx2 = null;
      try {
         tx = session.beginTransaction();
         tx2 = sessionBackup.beginTransaction();
      
         [b]session.save(blog);[/b]
         [b]sessionBackup.save(blog);[/b]
         /*
         Blog bbkp = new Blog();
         BeanUtils.copyProperties(bbkp, blog);
         sessionBackup.save(bbkp);
                           */

         tx.commit();
         tx2.commit();
         
         log.info("Blog '" + name + "' persistido");

      } catch(JDBCException e){
         if (e.getErrorCode() == 1062){
            System.out.println("Ja existe um blog com esse nome no banco, escolha outro");         
         }

      } catch (HibernateException he) {
         if (tx != null) tx.rollback();
         if (tx2 != null) tx2.rollback();
         throw he;
               
      } finally {
         session.close();
         sessionBackup.close();
      }
      return blog;
   }



session.save(blog) works fine, but sessionbkp.save(blog) throws the following exception:

Code:
Illegal attempt to associate a collection with two open sessions


Please help me !! And about transaction, am I writing the right code ?

Thank you !

_________________
Carlos Magno
Java Developer - N


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 11:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not 2-PC !
you're explicitly using 2 tx. Most time, beeing in a 2-PC means, you must be in an app server.

Your exception is due to your double association of blog to 2 different sessions. This is not allowed.

In your (highly innefficent) case
Code:
session.save(blog);
session.flush();
session.evict(blog);
sessionBackup.save(blog);
session.flush();


But opening 2 tx is definitly not 2 phase commit.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 1:20 pm 
Newbie

Joined: Thu Nov 20, 2003 5:39 pm
Posts: 4
Location: Rio de Janeiro, Brazil
epbernard wrote:
This is not 2-PC !
you're explicitly using 2 tx. Most time, beeing in a 2-PC means, you must be in an app server.

Your exception is due to your double association of blog to 2 different sessions. This is not allowed.

In your (highly innefficent) case
Code:
session.save(blog);
session.flush();
session.evict(blog);
sessionBackup.save(blog);
session.flush();


But opening 2 tx is definitly not 2 phase commit.


So, how can I do a real 2 phase commit ????

_________________
Carlos Magno
Java Developer - N


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, this is highly not hibernate related, and I answer this forum for free.
I recommend you :
- to read your app server on 2-PC capabilities (datasource section)
- or search commercial support on that

BTW I consider 2-Pc evil so my very best piece of advice is not to use that ;-)

Good luck.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 1:53 pm 
Newbie

Joined: Thu Nov 20, 2003 5:39 pm
Posts: 4
Location: Rio de Janeiro, Brazil
epbernard wrote:
Hum, this is highly not hibernate related, and I answer this forum for free.
I recommend you :
- to read your app server on 2-PC capabilities (datasource section)
- or search commercial support on that

BTW I consider 2-Pc evil so my very best piece of advice is not to use that ;-)

Good luck.


Sorry, my question is: Is it possible to make 2-PC with hibernate ?? Can I implement 2-PC in Hibernate programmaticly ??

Thank you for your help !! I really appreciate it.

_________________
Carlos Magno
Java Developer - N


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 4:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
One sessionfactory in hibernate can only work with a connection to one database at a time.

Thus, to get any kind of 2 PC, you should use 2 sessionfactories (which does allow having entities referring across each other)

Secondly - 2 PC is best used inside a J2EE container, because that is what they do!

(so the short answer is: No, Hibernate does not implement 2 PC - we do ORM)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 7:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Just to reiterate what everyone else is saying:

(1) of course you can use Hibernate in an environment where two-phase commit is required
(2) no, Hibernate does not itself implement 2PC - you need some other JTA implementation


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.