📄 sessionmanager.java
字号:
package com.student.dao;
import java.io.PrintStream;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
public class SessionManager
{
private SessionManager()
{
try
{
if(sf == null)
synchronized(lockObj)
{
if(sf == null)
sf = (new Configuration()).configure().buildSessionFactory();
}
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
public static SessionManager getInstance()
{
return new SessionManager();
}
public Session getSession()
{
Session ss = null;
try
{
ss = sf.openSession();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
return ss;
}
public Session openSession()
{
Session ss = (Session)currentThread.get();
if(ss == null)
{
try
{
ss = sf.openSession();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
currentThread.set(ss);
}
if(!ss.isConnected() || !ss.isOpen())
try
{
ss.reconnect();
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("=======================================");
System.out.println("Session have been closed");
System.out.println("Session have been opened now!");
}
catch(HibernateException ex1)
{
System.out.println("Session have been closed");
ex1.printStackTrace();
}
return ss;
}
public void closeSession()
{
Session ss = (Session)currentThread.get();
currentThread.set(null);
try
{
if(ss != null)
ss.close();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
private static SessionFactory sf = null;
private static final ThreadLocal currentThread = new ThreadLocal();
private static Object lockObj = new Object();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -