在java timer定时器中调用dao层会报错"No Session found for current thread"

yanxin9210 2016-03-29 05:11:06
现在在做一个web项目,使用spring+springMVC+hibernate框架,

问题描述:
项目中有一个扫描的功能,分即时任务、定时任务、周期任务三种,即时任务已经实现了,现在使用java.util.Timer、java.util.TimerTask类做定时任务,重写TimerTask的run方法,在run方法中调用之前已经没有问题的即时任务扫描方法。每次运行定时任务时,会卡在dao层方法的调用上,因为在即时任务的扫描方法中调用了dao层的数据操作方法,报错“No Session found for current thread”。

即时任务的执行流程:前台提交扫描请求->控制器调用service层的立即扫描方法;
定时任务的执行流程:前台提交扫描请求->控制器调用service层的定时扫描方法->调用立即扫描方法。

下面附上代码和配置文件,请各位大牛帮忙解决啊!


service层代码


/**
* 开始定时扫描任务
* @param blTask
*/
public void startTimerTask(final BLTask blTask) {
Date startDate = blTask.getStartTime();
TimerTask task = new TimerTask() {

@Override
public void run() {
// 调用扫描方法
startImmediateTask(blTask);
}
};
Timer timer = new Timer();
timer.schedule(task, startDate);
}


/**
* 开始即时扫描任务
* @param blTask
*/
public void startImmediateTask(BLTask blTask) {

BLScanParam blScanParam = new BLScanParam();
BLPolicyGroup blPolicyGroup = null; // 策略组
List<BLCheckItem> blCheckItems = null; // 检查项
Map<Long, BLCheckScript> blCheckScriptMap = new HashMap<Long, BLCheckScript>(); // 检查脚本

Long pgId = null; // 策略组ID
String alias = null; // 策略别名、检查项别名
Long osType = blTask.getOsType(); // 操作系统类型
List<Long> csIds = new ArrayList<Long>(); // 检查脚本ID数组

blScanParam.setBlTask(blTask);

pgId = blTask.getPgId();
// 通过策略组ID查找策略组
blPolicyGroup = blPolicyGroupDao.get(pgId);

if (blPolicyGroup == null) {
return;
}
blScanParam.setBlPolicyGroup(blPolicyGroup);
alias = blPolicyGroup.getAlias();

// 通过别名、操作系统类别查找所有匹配的检查项
blCheckItems = blCheckItemDao.queryByProerties(new String[]{"alias", "osType"}, new Object[]{alias, osType});

if (blCheckItems == null || blCheckItems.size() <= 0) {
return;
}
blScanParam.setBlCheckItems(blCheckItems);

for (BLCheckItem blCheckItem : blCheckItems) {
csIds.add(blCheckItem.getCsId());
}
int size = csIds.size();

// 获取当前策略组下的所有检查项的检测脚本
List<BLCheckScript> blCheckScripts = blCheckScriptDao.queryByProerties("csId", (Long[])csIds.toArray(new Long[size]));

for (BLCheckScript blCheckScript : blCheckScripts) {
// 获取当前策略组下的所有检测脚本,并放入map中
blCheckScriptMap.put(blCheckScript.getCsId(), blCheckScript);
}

blScanParam.setBlCheckScriptMap(blCheckScriptMap);

// 分割ip为数组,传入TaskDispatch
String[] ips = getIps(blTask.getIpArea());
TaskDispatch taskDispatch = new TaskDispatch(ips, blScanParam);
taskDispatch.StartTask();

// 将当前TaskDispatch对象的引用放入taskDispatchMap中,供停止任务时使用
taskDispatchMap.put(blTask.getTkId(), taskDispatch);
}




applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:annotation-config />
<context:component-scan base-package="com.djbh" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.1.247:3306/djbh_db?useUnicode=true&characterEncoding=utf-8" />
<property name="user" value="root" />
<property name="password" value="mysql" />


<!--
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;DatabaseName=DJBH_DB" />
<property name="user" value="sa" />
<property name="password" value="123456" />
-->
<property name="acquireIncrement" value="3" />
<property name="idleConnectionTestPeriod" value="120" />
<property name="initialPoolSize" value="3" />
<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="15" />
<property name="numHelperThreads" value="3" />
<property name="preferredTestQuery" value="select 1" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.jdbc.fetch_size">30</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.djbh.model</value>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>

</beans>



springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


<mvc:annotation-driven/>

<mvc:resources location="/static/" mapping="/static/**"/>
<mvc:resources location="/attachment/report_html/" mapping="/attachment/report_html/**"/>

<context:component-scan base-package="com.djbh.controller" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/page/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--<property name="maxUploadSize" value="10485760"></property> -->
</bean>

<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>



...全文
213 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧