hibernate的SaveOrUpdateEventListener和spring的SecurityContextHolder的问题

hibernate 版本是3.6,springSecurity版本是3.0.5,spring版本是3.0.6,问题是
做了一个hibernate的listener,implements了SaveOrUpdateEventListener ,这个类会监控所有调用saveorupdate方法,然后在之前插入我的业务逻辑,如设置创建用户,公司等通用信息。平时也运行正常,但是偶尔会遇到没有插入用户,公司信息的情况,大概10w分之一的概率吧,代码如下:

@SuppressWarnings("serial")
public class AuditListener implements SaveOrUpdateEventListener {

private static Logger logger = LoggerFactory.getLogger(AuditListener.class);
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
Object object = event.getObject();
if (object instanceof AuditableEntity
&& SpringSecurityUtils.getCurrentCustomUser() != null) {
AuditableEntity entity = (AuditableEntity) object;
String loginName = SpringSecurityUtils.getCurrentUserName();
// Long id = getEntityId(entity);
Long id = entity.getMainid();
if (id == null) {
// 创建新对象
entity.setRecAddTime(new Date());
entity.setRecLoginUser(loginName);
entity.setCompanyId(SpringSecurityUtils.getCurrentCustomUser()
.getCompanyId());
entity.setSystemId(SpringSecurityUtils.getCurrentCustomUser()
.getSystemId());
logger.warn("{}对象(ID:{}) 被 {} 在 {} 创建",
new Object[] {
object.getClass().toString(), id,
loginName, new Date() });
} else {
// 修改旧对象
entity.setRecUpdateTime(new Date());
entity.setRecModifyUser(loginName);

logger.warn("{}对象(ID:{}) 被 {} 在 {} 修改",
new Object[] {
object.getClass().toString(), id,
loginName, new Date() });
}
}
}
}


public class SpringSecurityUtils {


/**
* 取得当前用户的User实例, 如果当前用户未登录则返回null.
*/
public static CustomerUserDetails getCurrentCustomUser() {
Authentication authentication = getAuthentication();
if (authentication != null && authentication.getPrincipal() != null
&& !authentication.getPrincipal().equals("anonymousUser"))
return (CustomerUserDetails) authentication.getPrincipal();
return null;
}


/**
* 取得Authentication, 如当前SecurityContext为空时返回null.
*/
private static Authentication getAuthentication() {
SecurityContext context = SecurityContextHolder.getContext();
if (context != null)
return context.getAuthentication();
return null;
}
}



我没有办法验证重复这个过程,hibernate应该不会出问题,只是猜测可能的情况是,用户在调用saveorupdate方法后,由于数据库延时,而导致卡住,但是这个时候,刚好session过期了,通过SecurityContextHolder无法获得用户信息,从而导致了设置用户,公司这个业务的失败,大家能不能帮我分析一下我的猜测是否正确,如何验证,又如何解决。部署在jboss5.01GA下没有这样的问题,但是月前部署在tomcat6.0.18下开始出现问题,会和tomcat有关吗,拿不到session?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值