【问题标题】:hibernate closing connections or not?休眠关闭连接与否?
【发布时间】:2013-04-29 18:29:25
【问题描述】:

我的代码如下。可能我以类似的方式多次使用它,即简单地说,我以这种方式管理会话和事务:

 List<Login> users= null;
        try{
            session=HibernateUtil.getSessionFactory().getCurrentSession();
            tx=session.beginTransaction();
            users=session.createQuery("from Login").list();
            tx.commit();
        }catch(Exception e){System.out.println("commit exception:"+e);
            try {tx.rollback();} catch (Exception ex) {System.out.println("rollback exception:"+ex);} 
        }finally{if(session!=null && session.isOpen()){session.close();}}
        return users;

现在,当我第一次运行数据库服务(使用 MySQL)并使用此查询从命令提示符检查时...

show status like 'Conn%';

...结果是:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 2     |
+---------------+-------+

当我启动我的应用程序并使用它时。打开几页并查询相同的内容后。我得到的连接数为 6,我什至看到了 20 以上。

现在我想知道hibernate是否正在关闭连接?

我正在以这种方式处理所有事务,我交叉检查并在没有关闭会话的情况下看到任何代码块。

Hibernate.cfg.xml

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/shareapp
        </property>
        <property name="connection.username">pluto</property>
        <property name="connection.password">admin</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>


    </session-factory>

hibernateutil 类

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Login.class);
        config.addAnnotatedClass(FilesInfo.class);
        config.addAnnotatedClass(FilesShare.class);

        config.configure("hibernate.cfg.xml");
     //   new SchemaExport(config).create(true,true);

        sessionFactory = config.buildSessionFactory();

        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

谢谢!

【问题讨论】:

  • 如何建立 JDBC 连接?您是否使用池连接?请发布数据源配置
  • 哦,对不起,我现在添加了它,请看一下。 :)
  • 你能把HibernateUtil类的代码也贴出来吗?
  • 那么有人会回答这个问题吗,我在 hibernate.cfg 中将数据库连接更改为 jndi 数据源,但仍然没有变化

标签: mysql hibernate connection


【解决方案1】:

“连接”状态变量只是指

与 MySQL 服务器的连接尝试次数(成功与否),

而不是活动连接的数量。 这是链接:http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Connections

要获取打开的连接数,请检查“Threads_connected”变量,记录在 http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Threads_connected

【讨论】:

  • 顺便说一句,如果您使用 c3po 进行连接池,您可能可以使用 Connection Customizers 来跟踪代码中的连接;更多解释可以找到here
猜你喜欢
  • 2013-09-05
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2015-06-08
  • 2010-12-31
  • 2014-12-14
相关资源
最近更新 更多