-->
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.  [ 2 posts ] 
Author Message
 Post subject: strange StackOverflowError!
PostPosted: Wed Jun 09, 2004 9:51 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.SessionBean.java

package com.yours.admin.web;

import java.util.List;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class SessionBean {

//是否已经登录
private boolean isLogon;

private UserActionForm userActionForm;
private TeamActionForm teamActionForm;
private RoleActionForm roleActionForm;
private AuthActionForm authActionForm;
private java.util.List authList;

/**
* 取得登录状态
* @return boolean
*/
public boolean getIsLogon() {
return isLogon;
}

/**
* 设置登录状态
* @param isLogon boolean
*/
public void setIsLogon(boolean isLogon) {
this.isLogon = isLogon;
}

/**
*
* @return UserActionForm
*/
public UserActionForm getUserActionForm() {
return userActionForm;
}

/**
*
* @param userActionForm UserActionForm
*/
public void setUserActionForm(UserActionForm userActionForm) {
this.userActionForm = userActionForm;
}
/**
*
* @return AuthActionForm
*/
public AuthActionForm getAuthActionForm() {
return authActionForm;
}
/**
*
* @param authActionForm AuthActionForm
*/
public void setAuthActionForm(AuthActionForm authActionForm) {
this.authActionForm = authActionForm;
}
/**
*
* @return RoleActionForm
*/
public RoleActionForm getRoleActionForm() {
return roleActionForm;
}
/**
*
* @param roleActionForm RoleActionForm
*/
public void setRoleActionForm(RoleActionForm roleActionForm) {
this.roleActionForm = roleActionForm;
}
/**
*
* @return TeamActionForm
*/
public TeamActionForm getTeamActionForm() {
return teamActionForm;
}
/**
*
* @param teamActionForm TeamActionForm
*/
public void setTeamActionForm(TeamActionForm teamActionForm) {
this.teamActionForm = teamActionForm;
}
/**
*
* @return List
*/
public java.util.List getAuthList() {
return authList;
}
/**
*
* @param authList List
*/
public void setAuthList(java.util.List authList) {
this.authList = authList;
}

}



3.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);
}
}
}
}
}




3.dao

package com.yours.admin.dao;

import java.util.List;
import java.util.Iterator;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.apache.log4j.Logger;
import com.yours.admin.jdo.Auth;
import com.yours.datamodel.hibernate.*;

/**
* <p>Title: DAO</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 段洪杰
* @version 1.0
*/

public class AuthDAOImpl implements AuthDAO{


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


/**
* 增加记录
* @param auth Auth
*/
public void setAuth(Auth auth){
try {
Session session = HibernateSession.currentSession();
HibernateSession.currentTransaction();
session.save(auth);
session.flush();
}
catch (HibernateException e) {
log.error(e);
}
}

/**
* 通过ID取得记录
* @param id String
* @return Auth
*/
public Auth getAuthById(String id){
try {
Session session = HibernateSession.currentSession();
return (Auth) session.load(Auth.class, id);
}
catch (HibernateException e) {
log.error(e);
return null;
}
}

/**
* 修改记录
* @param Auth auth
*/
public void modifyAuth(Auth auth){
try {
Session session = HibernateSession.currentSession();
HibernateSession.currentTransaction();
session.update(auth);
session.flush();
}
catch (HibernateException e) {
log.error(e);
}
}

/**
* 删除记录
* @param Auth auth
*/
public void removeAuth(Auth auth){
try {
Session session = HibernateSession.currentSession();
HibernateSession.currentTransaction();
session.delete(auth);
session.flush();
}
catch (HibernateException e) {
log.error(e);
}
}
/**
* 取记录总数
* @return int
*/
public int getAuthsCount(){
int count = 0;
try {
Session session = HibernateSession.currentSession();
count = ( (Integer) session.iterate("select count(*) from Auth").next()).intValue();
}
catch (HibernateException e) {
log.error(e);
}
return count;
}

/**
* 取记录集合
* @return Iterator
* @param int position, int length
*/
public Iterator getAuths(int position, int length){
Iterator iterator=null;
String queryString =" select auth from Auth as auth order by auth.id desc";
try {
Session session = HibernateSession.currentSession();
//创建查询
Query query = session.createQuery(queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
}
catch (HibernateException e) {
log.error(e);
}
return iterator;
}

}



4. struts-actionBean

package com.yours.admin.web;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.Date;
import com.yours.admin.dao.AuthDAO;
import com.yours.admin.dao.AuthDAOFactory;
import com.yours.admin.dao.AuthDAOImpl;
import com.yours.admin.map.AuthMap;
import com.yours.admin.jdo.Auth;
import org.apache.log4j.Logger;
import com.yours.admin.tree.TreeAuthFacade;


public class SetAuthAction extends Action {

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

public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

AuthActionForm authActionForm = (AuthActionForm) actionForm;
AdminSessionBean adminSessionBean = (AdminSessionBean) httpServletRequest.getSession().getAttribute("adminSessionBean");
if (adminSessionBean == null) {
httpServletRequest.setAttribute("message", "系统超时,请返回!");
httpServletRequest.setAttribute("locationFile","history.go(-2);");
return actionMapping.findForward("message");
}
if (!adminSessionBean.getIsLogon()) {
httpServletRequest.setAttribute("message", "没有登录!");
httpServletRequest.setAttribute("locationFile","location='adminLogon.jsp';");
return actionMapping.findForward("message");
}

String backURL=httpServletRequest.getHeader("Referer");

////////////数据入库////////////
AuthDAO authDAO = null;
Auth auth = null;
try {
authDAO = AuthDAOFactory.getAuthDAO();
auth= new AuthMap().getAuth(authActionForm);

auth.setCreatedate(new Date());
authDAO.setAuth(auth);

new TreeAuthFacade().createAuthTreeJavaScript(httpServletRequest.getRealPath("/admin"));

}
catch (Exception ex) {
log.error(ex);
}
httpServletRequest.setAttribute("message", "录入完成!");
httpServletRequest.setAttribute("locationFile","location='"+backURL+"';");
return actionMapping.findForward("message");
}
}


4.Struts-plusIn
package com.iclass.plugin;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;

import com.iclass.filtrate.Filtrate;

public class InitFiltrate implements PlugIn {

private static Filtrate filtrate=new Filtrate();
private static Logger log = Logger.getLogger(InitFiltrate.class);
private String fileName;
/**
*
* @param servlet ActionServlet
* @param config ModuleConfig
* @throws ServletException
*/
public void init(ActionServlet servlet, ModuleConfig config) throws
ServletException {

try {
String realFileName = servlet.getServletContext().getRealPath(fileName);
filtrate.setFileName(realFileName);
}
catch (Exception ex) {
log.error(ex+" 初始化Filtrate时出错!\r\n");
}

}
/**
*
*/
public void destroy() {
filtrate = null;
}
/**
*
* @return Filtrate
*/
public static Filtrate getFiltrate() {
return filtrate;
}
/**
*
* @return String
*/
public String getFileName() {
return fileName;
}
/**
*
* @param fileName String
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 10:01 pm 
Newbie

Joined: Wed Jun 09, 2004 9:19 pm
Posts: 4
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());
}
}
}


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

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.