HibernateDaoSupport 記憶體洩漏的問題!
為什麼在控制檯會提示這樣的資訊:
WARN [ConnectionManager] unclosed connection, forgot to call close() on your session?
----------------------------------------------------------------
我是用spring+hibernate架構的,在資料庫訪問類DAO中extends HibernateDaoSupport。
我是用myeclipse做的,它幫我自動生成了SessionFactory類:
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/xxx/WebRoot/WEB-INF/applicationContext.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
--------------------------------------------------------
DAO基本都是這樣的:
public class TAreaDAO extends HibernateDaoSupport {
public TAreaDAO(){
super();
}
.....
.....
public static TAreaDAO getFromApplicationContext (ApplicationContext ctx) {
return (TAreaDAO) ctx.getBean("TAreaDAO");
}
}
WARN [ConnectionManager] unclosed connection, forgot to call close() on your session?
----------------------------------------------------------------
我是用spring+hibernate架構的,在資料庫訪問類DAO中extends HibernateDaoSupport。
我是用myeclipse做的,它幫我自動生成了SessionFactory類:
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/xxx/WebRoot/WEB-INF/applicationContext.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
--------------------------------------------------------
DAO基本都是這樣的:
public class TAreaDAO extends HibernateDaoSupport {
public TAreaDAO(){
super();
}
.....
.....
public static TAreaDAO getFromApplicationContext (ApplicationContext ctx) {
return (TAreaDAO) ctx.getBean("TAreaDAO");
}
}
相關文章
- AFN的記憶體洩漏問題記憶體
- ThreadLocal記憶體洩漏問題thread記憶體
- redisson記憶體洩漏問題排查Redis記憶體
- 關於PHP記憶體洩漏的問題PHP記憶體
- iOS八種記憶體洩漏問題iOS記憶體
- iOS 11 textField記憶體洩漏問題iOS記憶體
- bea記憶體洩漏問題診斷記憶體
- 記憶體洩漏問題分析之非託管資源洩漏記憶體
- BufferedImage記憶體洩漏和溢位問題記憶體
- 記憶體洩漏記憶體
- 解決記憶體洩漏(1)-ApacheKylin InternalThreadLocalMap洩漏問題分析記憶體Apachethread
- 記憶體洩漏引起的 資料庫效能問題記憶體資料庫
- 如何解決JVM OutOfMemoryError記憶體洩漏問題?JVMError記憶體
- 分析記憶體洩漏和goroutine洩漏記憶體Go
- 記憶體洩漏的原因記憶體
- 【Java面試題】之記憶體洩漏Java面試題記憶體
- js記憶體洩漏JS記憶體
- Java記憶體洩漏Java記憶體
- webView 記憶體洩漏WebView記憶體
- Javascript記憶體洩漏JavaScript記憶體
- 分析ThreadLocal的弱引用與記憶體洩漏問題thread記憶體
- 對於記憶體洩漏問題的簡單認知記憶體
- 我是如何搞定 NodeJS 記憶體洩漏問題的NodeJS記憶體
- WebView引起的記憶體洩漏WebView記憶體
- ARC下的記憶體洩漏記憶體
- 【轉】Java的記憶體洩漏Java記憶體
- 使用 Chrome Dev tools 分析應用的記憶體洩漏問題Chromedev記憶體
- handlder引起的記憶體洩漏問題以及解決辦法記憶體
- ThreadLocal會不會有所謂“記憶體洩漏”的問題thread記憶體
- 急!請教用optimizeit檢測記憶體洩漏的問題?記憶體
- 記憶體分析與記憶體洩漏定位記憶體
- 記憶體洩漏和記憶體溢位記憶體溢位
- valgrind 記憶體洩漏分析記憶體
- Android 記憶體洩漏Android記憶體
- Android記憶體洩漏Android記憶體
- 淺談記憶體洩漏記憶體
- JavaScript 記憶體洩漏教程JavaScript記憶體
- 說說 記憶體洩漏記憶體