-->
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.  [ 2 posts ] 
Author Message
 Post subject: hibernate w/derby's restore feature; how to make work?
PostPosted: Thu Feb 09, 2006 3:33 am 
Newbie

Joined: Thu Feb 09, 2006 3:12 am
Posts: 2
Using Derby and Hibernate.

Derby has a backup and restore feature.

The backup feautre works as documented by using a connection's 'prepareCall' method.

Derby will restore the database using a modified connection URL of the form: 'jdbc:derby:testDatabase;restoreFrom=c:\mybackups\testDatabase'.

Placing this modified URL into the hibernate.cfg.xml file and re-running my application restores the database.

But, I don't want to have to shutdown my application, modify the hibernate.cfg.xml file and restart to get the backup restored.

I thought the key to make it work would be to shut down all hibernate connections I could think off such as session.close, flush, etc.

I then discarded my session factory and made a new one, however I first swapped my new URL for the old in the properties collection before I made the new session factory - like this:

Configuration configuration = new Configuration().configure();
configuration.setProperty("hibernate.connection.url", "jdbc:derby:testDatabase;restoreFrom=c:\mybackups\testDatabase");
sessionFactory = configuration.buildSessionFactory();

To bad for me, new session from the session factory works just fine, but the resulting information is not from the restored database but from the current one. It is like the actual database is never really closed and re-opened using the new URL.

Has anyone done this before or have clue how hibernate/JDBC might be out smarting me?

Thanks for your help.

kstotz


Top
 Profile  
 
 Post subject: Answer
PostPosted: Fri Feb 10, 2006 2:16 am 
Newbie

Joined: Thu Feb 09, 2006 3:12 am
Posts: 2
For public reference here is an answer I came up with:

Code:
  public static void restoreDatabase() throws HibernateException
  {
      String backupPath = "c:/databaseBackup";
      HibernateUtil.currentSession().flush();
      HibernateUtil.currentSession().disconnect();
      HibernateUtil.closeSession();

      try {
        DriverManager.getConnection("jdbc:derby:sampleDB;shutdown=true");
      }
      catch (SQLException ex) {
        System.out.println(ex.toString());
      }

      sessionFactory.close();

      Configuration configuration = new Configuration().configure();
      String value = configuration.getProperty("hibernate.connection.url");
      configuration.setProperty("hibernate.connection.url", value + ";restoreFrom=" + backupPath + "/sampleDB");
      sessionFactory = configuration.buildSessionFactory();
  }


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