+ -
当前位置:首页 → 问答吧 → 关于hibernate的session问题

关于hibernate的session问题

时间:2011-11-16

来源:互联网

public class HibernateUtil
{
private static final SessionFactory sessionFactory;
private static final ThreadLocal session = new ThreadLocal();
private static final ThreadLocal tLocaltx = new ThreadLocal();

static {
try { sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static Session currentSession() throws HibernateException {
Session s = (Session)session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set;
try {
s.connection().setAutoCommit(true);
}
catch (SQLException e) {
  
e.printStackTrace();
throw new HibernateException(e.getMessage());
}
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session)session.get();
session.set(null);
if (s != null)
s.close();
}

public AssiCommentsPk insert(AssiComments dto) throws AssiCommentsDaoException
  {
    try {
      Session session = HibernateUtil.currentSession();
    
      // create and populate the hibernate object
      com.et.oa.hibernate.AssiComments hibernate = new com.et.oa.hibernate.AssiComments();
      hibernate.setTname( dto.getTname() );
      hibernate.setPtime( dto.getPtime() );
      hibernate.setSenders( dto.getSenders() );
      hibernate.setContents( dto.getContents() );
      hibernate.setStatus( dto.getStatus() );
      hibernate.setRemark( dto.getRemark() );
    
      session.save(hibernate);
      session.flush();
      // return the DTO pk object
      return new AssiCommentsPk(hibernate.getId());
    }
    catch (Exception _e) {
      System.out.println( _e );
      throw new AssiCommentsDaoException( _e.getMessage() );
    }
    
  }

public WfProcdefPk insert(WfProcdef dto) throws WfProcdefDaoException
  {
    try {
      Session session = HibernateUtil.currentSession();
    
      // create and populate the hibernate object
      com.et.oa.hibernate.WfProcdef hibernate = new com.et.oa.hibernate.WfProcdef();
      hibernate.setName( dto.getName() );
      hibernate.setDes( dto.getDes() );
      hibernate.setCode( dto.getCode() );
      hibernate.setStatus( dto.getStatus() );
      hibernate.setXml( dto.getXml() );
      hibernate.setType( dto.getType() );
      hibernate.setRemark( dto.getRemark() );
      hibernate.setUpdatetime( dto.getUpdatetime() );
    
      HibernateUtil.beginTransaction();
      session.save(hibernate);
      HibernateUtil.commitTransaction();
      HibernateUtil.closeSession();
      // return the DTO pk object
      return dto.createPk();
    }
    catch (Exception _e) {
      System.out.println( _e );
      throw new WfProcdefDaoException( _e.getMessage() );
    }
    
  }

hibernate的配置文件
  <property name="connection.driver_class">
    com.microsoft.sqlserver.jdbc.SQLServerDriver
  </property>
  <property name="connection.url">
    jdbc:sqlserver://localhost:1433;DatabaseName=etoa
  </property>
  <property name="connection.username">sa</property>
  <property name="connection.password">123456</property>
  <property name="hibernate.connection.provider_class">
    org.hibernate.connection.C3P0ConnectionProvider
  </property>
  <!-- 最大连接数 -->
  <property name="hibernate.c3p0.max_size">500</property>
  <!-- 最小连接数 -->
  <property name="hibernate.c3p0.min_size">80</property>
  <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 ***-->
  <property name="hibernate.c3p0.timeout">3600</property>
  <!-- 最大的PreparedStatement的数量 -->
  <property name="hibernate.c3p0.max_statements">200</property>
  <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
  <property name="hibernate.c3p0.idle_test_period">120</property>
  <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
  <property name="hibernate.c3p0.acquire_increment">10</property>
  <!-- 每次都验证连接是否可用 -->
  <property name="hibernate.c3p0.validate">true</property>
  <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
  </property>
  <property name="show_sql">true</property>
  <property name="myeclipse.connection.profile">
    com.microsoft.sqlserver.jdbc.SQLServerDriver
  </property>

代码有的调用是:

有的是:
HibernateUtil.beginTransaction();
session.save(hibernate);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();

有的是:
session.save(hibernate);
session.flush();

这样是不是有问题?

服务运行一段时间,tomcat就会报
[ERROR] 31 十月 10:45:16.125 上午 http-80-26 [org.hibernate.util.JDBCExceptionReporter]
You can't operate on a closed connection!!!

[ERROR] 31 十月 10:45:16.125 上午 http-80-26 [org.hibernate.event.def.AbstractFlushingEventListener]
Could not synchronize database state with session

麻烦大大们帮我看看吧!实在是对hibernate不了解啊

作者: liuliu   发布时间: 2011-11-16

麻烦大大们帮我看看吧!实在是对hibernate不了解啊

作者: kentyeh   发布时间: 2011-11-16

找了,但是不清楚

作者: liuliu   发布时间: 2011-11-16