如何使用Hibernate从数据库Derby中关闭?

时间:2019-05-23 01:14:39

标签: java hibernate derby

在JAVA-SWING-HIBERNATE-DERBY应用程序中,我需要在应用程序执行结束时从数据库断开连接,因为这里(https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html)和此处({ {3}}。

我解决了设置属性(derby.language.sequence.preallocator的问题(增加100),但是我想遵循文档并正确地关闭数据库。

我有一个配置文件,其中定义了连接属性

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,突出显示连接方法

public void connect(){
    try {
        session = HibernateConnector.getInstance().getSession();                
    }
    catch (Exception e) {           
        e.printStackTrace();
    } finally {
        session.close();
    }
}

但是,如何以一种优雅的方式断开连接?我想到了类似下面的代码,但它不起作用。

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();
    }
}

一些提示?

0 个答案:

没有答案