-->
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.  [ 1 post ] 
Author Message
 Post subject: edit hibernate.cfg at runtime
PostPosted: Sat Oct 01, 2011 8:59 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Dear all,
I've this question, please help me.
I've a java application that uses Hibernate.
Basing on a configuration file into application folder a daabase must be runned, that is derby if local version, mysql if network version.
I acted in this way:
1) I put into hibernate.cfg configuration of local version
2) when application runs, I check file and I change configuration at runtime.

This is my class

Code:
public class HibernateUtil {

    private static SessionFactory sessionFactory;// = buildSessionFactory();

    static {
        try {
            sessionFactory = buildSessionFactory();
            if (sessionFactory == null) {
                throw new Exception("No Session Factory");
            }
        } catch (Exception e) {
            System.out.println("Eccezione nella creazione del session factory : " + e.getMessage());
            e.printStackTrace();
        }

    }

    private static SessionFactory buildSessionFactory() {
        try {
            //leggo il file di configurazione dal file (quando faccio le modifiche nelle impostazioni
            //le salvo nel file
            ConfigDBMng.LoadOrCreateConfig();
            //  sessionFactory.close();
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
            sessionFactory.close();
            sessionFactory = null;

           

            AnnotationConfiguration conf = new AnnotationConfiguration().configure();
            // <!-- Database connection settings -->
            conf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            conf.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");

            conf.setProperty("hibernate.connection.url", "jdbc:mysql://" + ConfigDBMng.Path + ":" + ConfigDBMng.Port + "/" + ConfigDBMng.Database);
            conf.setProperty("hibernate.connection.username", "root");
            conf.setProperty("hibernate.connection.password", "");

           

            sessionFactory = conf.configure().buildSessionFactory();

       
            return sessionFactory;
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

   
    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            sessionFactory = buildSessionFactory();
        }

        return sessionFactory;
    }
}


What happens is that in the log I see the first is opened db from configuration of hibernate and later is opened correct db (from file)...but in the application I see data of first db, not the second one...of can I do ?
Please help me


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

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.