I found a way to solve my issue. The thing is that when I wanted to use the new configuration in the rest of the code I couldn't, 'cause for each transaction I opened a new session (as recommended by hibernate), but the that session was always the one that was at the beginning of the hibernate.cfg.xml file. Also, I was defining my configuration function in one button.
Now I changed the position of my function and put it in HibernateUtil.java, adding just the configurations that I needed and some that could be useful later
public static void SetSessionFactory(String url, String user, String pass) {
try {
AnnotationConfiguration conf = new AnnotationConfiguration().configure();
// <!-- Database connection settings -->
conf.setProperty("hibernate.connection.url", url);
conf.setProperty("hibernate.connection.username", user);
conf.setProperty("hibernate.connection.password", pass);
sessionFactory = conf.buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
And then, any time that I want to access to that new connection, at the beginning of each transaction I call the session pointed to the same class HibernateUtil.java
public Session session = HibernateUtil.getSessionFactory().openSession();
Without putting the first function in this class, the session that was being opened was always the one that was by default in the configuration file.