使用 Struts 存储 Hibernate SessionFactory
我开始将 Hibernate 与 Struts 2 一起用于一个相对简单的 Web 项目。出于性能原因,我知道建议尽量减少创建 Hibernate Configuration 和 SessionFactory 对象的时间。
任何人都可以提供一些意见,说明这是否是一个好方法,或者是否有更好的方法?我基于 我在这里找到的示例 。
方法是在ServletContextListener的contextInitialized方法中创建SessionFactory并将其存储在ServletContext中。
我注意到该示例似乎从未关闭 SessionFactory,因此我在 contextDestroyed 中添加了一些代码。这有必要吗?
非常感谢您的任何意见。如果您能提出任何更好的示例,我很乐意查看。我还看到了一些关于 Struts 的“Full Hibernate Plugin”的参考。这是一种常用且更好的方法吗?
FWIW,我正在使用 Eclipse 并使用 MySQL 部署到 Tomcat
public class HibernateListener implements ServletContextListener {
private Configuration config;
private SessionFactory sessionFactory;
private String path = "/hibernate.cfg.xml";
public static final String KEY_NAME = HibernateListener.class.getName();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
URL url = HibernateListener.class.getResource(path);
config = new Configuration().configure(url);
sessionFactory = config.buildSessionFactory();
// save the Hibernate session factory into serlvet context
arg0.getServletContext().setAttribute(KEY_NAME, sessionFactory);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
这是我添加到 web.xml 的内容
<listener>
<listener-class>insert.package.name.here.HibernateListener</listener-class>
</listener>
I'm getting started using Hibernate with Struts 2 for a relatively simply web project. For performance reasons, I know it is recommended to minimize the times you create the Hibernate Configuration and SessionFactory objects.
Can anyone provide some input on whether this is a good way to do it or if there are better approaches? I'm basing this code of an example I found here.
The approach is to create the SessionFactory in the contextInitialized method of the ServletContextListener and to store it in the ServletContext.
I notice the example doesn't seem to ever close the SessionFactory so I added some code in the contextDestroyed. Was this necessary?
Thanks much for any input. If you can suggest any better examples I'd be happy to look at them. I've also seen some references to a "Full Hibernate Plugin" for Struts. Is that a commonly used and better approach?
FWIW, I'm using Eclipse and deploying to Tomcat with MySQL
public class HibernateListener implements ServletContextListener {
private Configuration config;
private SessionFactory sessionFactory;
private String path = "/hibernate.cfg.xml";
public static final String KEY_NAME = HibernateListener.class.getName();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
URL url = HibernateListener.class.getResource(path);
config = new Configuration().configure(url);
sessionFactory = config.buildSessionFactory();
// save the Hibernate session factory into serlvet context
arg0.getServletContext().setAttribute(KEY_NAME, sessionFactory);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Here's what I added to the web.xml
<listener>
<listener-class>insert.package.name.here.HibernateListener</listener-class>
</listener>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法会起作用,并且
ServletContextListener
是处理 Web 应用程序的启动和关闭任务的正确位置。您在关闭时关闭 SessionFactory 是正确的 - 自己清理是一个好习惯。要考虑的另一件事是如何创建和处置会话。会话不应在线程之间共享,也不应在每个数据库任务上创建和销毁它们。一种常见的最佳实践是每个请求有一个会话(通常存储在 ThreadLocal 中)。这通常称为“在视图中打开会话”模式。
就我个人而言,我使用 Google Guice 的 guice-persist 扩展的稍微修改版本。
Your approach would work and
ServletContextListener
s are the right place to handle start-up and shut-down tasks for your webapp. You are correct in closing theSessionFactory
at shut-down — cleaning up after yourself is a good habit to be in.Another thing to consider is how you are creating and disposing of the sessions. Sessions should not be shared across threads nor should they be created and destroyed on every single database task. A common best practice is to have one session per request (often stored in a ThreadLocal). This is usually referred to as the Open Session in View pattern.
Personally, I use a slightly modified version of the guice-persist extension to Google Guice.