定时器 使用hibernate定时查询数据库

1.<!-- Hibernate配置 -->
2.<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
3.<property name="dataSource" ref="dataSource" />
4.<property name="namingStrategy">
5.<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
6.</property>
7.<property name="hibernateProperties">
8.<props>
9.<prop key="hibernate.dialect">${hibernate.dialect}</prop>
10.<prop key="hhibernate.autoReconnect">${hibernate.autoReconnect}</prop>
11.<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
12.<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
13.<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
14.<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
15.
16.//之前获取hibernate session 空指针 在配置里加了这2个属性就OK了 但是 dao查询时候必须要开启事务,sessionFactory.getCurrSession() 获取当前session
17.
18.<prop key="hibernate.current_session_context_class">thread</prop>
19.<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
20.</props>
21.</property>
22.<property name="packagesToScan">
23.<list>
24. <value>com.unionpay.portal</value>
25. </list>
26.</property>
27.</bean>

 

<!-- 服务器启动定时器 首先要在 web.xml 监听-->
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ApplicationListener implements ServletContextListener {


public void contextDestroyed(ServletContextEvent arg0) {


}


public void contextInitialized(ServletContextEvent arg0) {
//new 出一个时间探测器
Timer t=new Timer();
//每隔一定时间执行任务
t.schedule(new MyTask(), 0, 30*1000);
}

}

MyTask 为自定义类。继承timer? schedule第一个参数类型


//============================================================

//以上配置有误,这样配置会出现,定时器查询session 和 正常启动页面session 冲突。导致页面500 获取不到session而定时器则可以获取。

//修改为 将以上2 配置属性去除, 改为this.sessionFactory.open() , session.close();手动开始和关闭session


dao 类 继承 HibernateDao
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值