in a JAVA-SWING-HIBERNATE-DERBY application, I need to disconnect from the database at the end of the application execution, as there is a sequence problem, as documented here (https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html) and here (Derby Auto Increment by 100 when specified as 1).
I solved this issue (increment by 100) setting the property (derby.language.sequence.preallocator
), but I would like to follow the documentation and shutdown the database correctly.
I have a configuration file, in which I define the connection properties
HibernateConnector.java
public class HibernateConnector {
private static HibernateConnector me;
private Configuration cfg;
private SessionFactory sessionFactory;
private StringBuilder derbyPath;
public void setDerbyPath(StringBuilder derbyPath) {
this.derbyPath = derbyPath;
}
private HibernateConnector() throws HibernateException {
cfg = new Configuration();
/**
* Connection Information..
*/
System.setProperty("derby.system.home", System.getProperty("user.dir") + "\\db");
System.setProperty("derby.language.sequence.preallocator", "1");
cfg.setProperty("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver");
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyTenSevenDialect");
cfg.setProperty("hibernate.connection.username", "myUser");
cfg.setProperty("hibernate.connection.password", "123");
derbyPath = new StringBuilder("jdbc:derby:myDatabase");
derbyPath.append(";createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8");
derbyPath.append(";useTimezone=true");
derbyPath.append(";serverTimezone=UTC");
cfg.setProperty("hibernate.connection.url", derbyPath.toString());
//cfg.setProperty("hibernate.hbm2ddl.auto", "create");
//cfg.setProperty("hibernate.show_sql", "true");
/**
* Mapping Resources..
*/
cfg.addAnnotatedClass(br.pro.x87.model.Marca.class);
sessionFactory = cfg.buildSessionFactory();
}
public static synchronized HibernateConnector getInstance() throws HibernateException {
if (me == null) {
me = new HibernateConnector();
}
return me;
}
public Session getSession() throws HibernateException {
Session session = sessionFactory.openSession();
if (!session.isConnected()) {
this.reconnect();
}
return session;
}
private void reconnect() throws HibernateException {
this.sessionFactory = cfg.buildSessionFactory();
}
}
DAO.java, highlighting the connect method
public void connect(){
try {
session = HibernateConnector.getInstance().getSession();
}
catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
But, how can I disconnect, in an elegant way? I thought of something like the code below but it does not work.
public void disconnect(){
StringBuilder derbyPath = new StringBuilder("jdbc:derby:myDatabase");
derbyPath.append(";createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8");
derbyPath.append(";useTimezone=true");
derbyPath.append(";serverTimezone=UTC");
derbyPath.append(";shutdown=true");
System.out.println("Desconectando...");
try {
HibernateConnector.getInstance().setDerbyPath(derbyPath);
session = HibernateConnector.getInstance().getSession();
}
catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
Some tip?