问题描述
我开始使用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 {
$ b $ private private config config;
private SessionFactory sessionFactory;
private String path =/hibernate.cfg.xml;
public static final String KEY_NAME = HibernateListener.class.getName();
$ b $ @Override
public void contextDestroyed(ServletContextEvent arg0){
if(sessionFactory!= null){
sessionFactory.close();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0){
try {
URL url = HibernateListener .class.getResource(路径);
config = new Configuration()。configure(url);
sessionFactory = config.buildSessionFactory();
//将Hibernate会话工厂保存到serlvet上下文
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>
ServletContextListener s是处理web应用程序的启动和关闭任务的正确位置。您在关闭时关闭 SessionFactory
是正确的—清理自己是一种好习惯。
另一个要考虑的问题是如何创建和处理会话。会话不应该跨线程共享,也不应该在每个单一的数据库任务上创建和销毁。常见的最佳做法是每个请求有一个会话(通常存储在一个ThreadLocal中)。这通常被称为在视图中打开会话模式。
就个人而言,我使用稍微修改过的guice-persist扩展版本Google Guice。
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>
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 the SessionFactory
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.
这篇关于用Struts存储Hibernate SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!