0

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?

  • What does "it does not work" mean? Does your code fail to compile? Do you get an exception? Does it run to completion, but not seem to do what you expected it to? Exactly what does it do? – Bryan Pendleton May 23 '19 at 13:11
  • The database keeps running. As a matter of fact, using "disconnect" I can't open the program again. I believe that changes the connection properties. – Juliano Rodrigo Lamb May 23 '19 at 13:47
  • I cannot connect again, using disconnect method – Juliano Rodrigo Lamb May 23 '19 at 13:49
  • Yes, it seems like the setDerbyPath() call in your disconnect method might interfere with subsequent connection attempts, so you probably have to set the derby path for your connector back to its original setting before you try to connect again? But do you get an exception when you call the disconnect method? BTW, your code is "swallowing" exceptions, which will make it hard to diagnose its behavior. In your catch blocks, you print the exception but then you just continue processing, which leads to very confusing program behavior. – Bryan Pendleton May 23 '19 at 20:10

0 Answers0