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