public class HibernateSessionFactory {
private static SessionFactory factory=null;
private static Session session=null;
static {
Configuration conf=new Configuration().configure();
factory=conf.buildSessionFactory();
}
public static SessionFactory getFactory() {
return factory;
}
public static Session getSession(){
session=factory.openSession();
return session;
}
public static void closeSession(){
if(session!=null){
session.close();
}
}
}public abstract class BaseHibernateDAO {
private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.save(item);
tx.commit();
} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.update(item);
tx.commit();
} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();
} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){
try {
List results=this.getSession().createCriteria(clazz).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected Session getSession() {
this.session=HibernateSessionFactory.getSession();
return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}
}
public abstract class BaseHibernateDAO {
private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.save(item);
tx.commit();
} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.update(item);
tx.commit();
} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();
} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){
try {
List results=this.getSession().createCriteria(clazz).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected Session getSession() {
this.session=HibernateSessionFactory.getSession();
return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}
}我第一次执行的是search()方法,session在finally里就关闭了,但是在之后执行delete()方法是,他就报错说session is already closed,但是我在delete()方法里有getSession()呀
怪事!
private static SessionFactory factory=null;
private static Session session=null;
static {
Configuration conf=new Configuration().configure();
factory=conf.buildSessionFactory();
}
public static SessionFactory getFactory() {
return factory;
}
public static Session getSession(){
session=factory.openSession();
return session;
}
public static void closeSession(){
if(session!=null){
session.close();
}
}
}public abstract class BaseHibernateDAO {
private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.save(item);
tx.commit();
} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.update(item);
tx.commit();
} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();
} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){
try {
List results=this.getSession().createCriteria(clazz).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected Session getSession() {
this.session=HibernateSessionFactory.getSession();
return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}
}
public abstract class BaseHibernateDAO {
private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.save(item);
tx.commit();
} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.update(item);
tx.commit();
} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
Session session=this.getSession();
try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();
} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){
try {
List results=this.getSession().createCriteria(clazz).list();
return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected Session getSession() {
this.session=HibernateSessionFactory.getSession();
return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}
}我第一次执行的是search()方法,session在finally里就关闭了,但是在之后执行delete()方法是,他就报错说session is already closed,但是我在delete()方法里有getSession()呀
怪事!
if(session!=null){
session.close();
}
}
把这个方法改成传一个Session session参数进去,然后里面关闭的是传进去的参数,然后在程序中要关闭某一个session的时候就传入该session到这个close方法!
不要定义成内部成员变量,应该在每次都需要获取的地方用getSession方法获得。
关闭的时候也调用共同方法closeSession(),但是要讲session作为参数传过去。
直接返回 factory.openSession() 试试吧
单用hibernate最好还是用ThreadLocal方式获得session
但是一涉及到一对多或者多对一的关联映射的配置的时候就出现session关闭问题!另外:你的代码发到帖子里面格式竟然还如在编辑器里面一模一样,不知道这个CSDN是怎么实现的! 我把代码粘贴到我的博客的时候发出来格式很难看,缩进对齐都不完整不知道怎么控制????