-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: (modify)strange StackOverflowError !
PostPosted: Wed Jun 09, 2004 10:03 pm 
Newbie

Joined: Wed Jun 09, 2004 9:19 pm
Posts: 4
All help me!

when i restart Tomcat5.0 after a day, it will happen "StackOverflowError"!

if i do not use Threadlocal,mybe it is OK!(do not affirm) why?

I use:

LINUX(REDHAD 8.0)
J2SDK 1.4.2_04
hibernate 2.14 2.13 2.12
tomcat 5.024 5.025 5.026
mysql 4.0
JDBC : mysql driver 3.07 stable


1.HibernateSession

package com.yours.datamodel.hibernate;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

import org.apache.log4j.Logger;

/**
*
* 获取Session连接工厂类
*
* @author Robbin Fan
*
*/
public class HibernateSession {

private static final ThreadLocal sessionThread = new ThreadLocal();

private static final ThreadLocal transactionThread = new ThreadLocal();

private static SessionFactory sf = null;

private static Logger log = Logger.getLogger(HibernateSession.class);


/**
* 获取当前线程使用的Session
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session s = (Session) sessionThread.get();
if (s == null) {
if (sf == null) sf = new Configuration().configure().buildSessionFactory();
s = sf.openSession();
sessionThread.set(s);
}
return s;
}

/**
* 获取一个新的Session
*
* @return Session
* @throws HibernateException
*/
public static Session newSession() throws HibernateException {
if (sf == null) sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
return s;
}

/**
* 启动或者加入当前Session的Transaction
*
* @return Transaction
* @throws HibernateException
*/
public static Transaction currentTransaction() throws HibernateException {
Transaction tx = (Transaction) transactionThread.get();
if (tx == null) {
tx = currentSession().beginTransaction();
transactionThread.set(tx);
}
return tx;
}

/**
* 提交当前Session的Transaction
*
* @throws HibernateException
*/
public static void commitTransaction() throws HibernateException {
Transaction tx = (Transaction) transactionThread.get();
transactionThread.set(null);
if (tx != null) {
tx.commit();
}
}

/**
* 回滚当前事务
*
* @throws HibernateException
*/
public static void rollbackTransaction() throws HibernateException {
Transaction tx = (Transaction) transactionThread.get();
transactionThread.set(null);
if (tx != null) tx.rollback();
}

/**
* 关闭当前线程使用的Session
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session s = (Session) sessionThread.get();
sessionThread.set(null);
if (s != null) {
s.close();
}
}

/**
* 根据映射文件和持久对象生成数据库DDL,生成文件为create_table.sql
*
* @param args 参数
*/
public static void main(String[] args) {
try {
String conf = "hibernate.cfg.xml";
if (args.length != 0) conf = args[0];
Configuration cfg = new Configuration().configure("/" + conf);
SchemaExport se = new SchemaExport(cfg);
se.setOutputFile("create_table.sql");
se.create(true,true);
} catch (HibernateException e) {
System.out.println(e.getMessage());
}
}
}


2.SetEncodingFilter


package com.iclass.util;

import com.yours.datamodel.hibernate.HibernateSession;
/**
*
* <p>Description: 解决汉字编码问题</p>
* <p>自动关闭 Hibernate Session</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 信息中心</p>
* @author 段洪杰
* @version 1.0
*/

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import net.sf.hibernate.HibernateException;

import org.apache.log4j.Logger;

public class SetEncodingFilter
implements Filter {

private static Logger log = Logger.getLogger(SetEncodingFilter.class);
protected String encoding = null;
protected FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}

public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
// Set Character Encoding
request.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}
finally {
// commit transaction and close hibernate session
try {
HibernateSession.commitTransaction();
}
catch (HibernateException e) {
try {
HibernateSession.rollbackTransaction();
}
catch (HibernateException e1) {}
log.error(e);
}
finally {
try {
// close Hibernate Session
HibernateSession.closeSession();
}
catch (HibernateException e) {
log.error(e);
}
}
}
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.