sách gpt4 ai đã đi

Chế độ ngủ đông có đóng kết nối không?

In lại 作者:行者123 更新时间:2023-11-29 03:05:26 28 4
mua khóa gpt4 Nike

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

 List users= null;
try{
session=HibernateUtil.getSessionFactory().getCurrentSession();
tx=session.beginTransaction();
users=session.createQuery("from Login").list();
tx. cam kết();
}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 是否正在关闭连接?

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

hibernate .cfg.xml







com.mysql.jdbc.Driver


jdbc:mysql://localhost:3306/shareapp

pluto
admin


1



org.hibernate.dialect.MySQLDialect



thread



org.hibernate.cache.NoCacheProvider



true


update



hibernate 工具类

public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
thử {
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;
}
}

Cảm ơn!

1 Câu trả lời

“连接”状态变量只是指

The number of connection attempts (successful or not) to the MySQL server,

而不是事件连接数。这是链接: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

关于mysql - 是否 hibernate 关闭连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285696/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com