简体   繁体   English

使用石英的Hibernate会话

[英]Using Hibernate session with quartz

I've a web application which uses framework like Struts and Hibernate. 我有一个使用Struts和Hibernate等框架的Web应用程序。 Currently I'm developing a scheduler for this application using Quartz. 目前我正在使用Quartz为这个应用程序开发一个调度程序。 While coding I realized that the use of Hibernate session is not possible with the threads of Quartz. 编码时我意识到使用Quartz的线程无法使用Hibernate会话。

Anybody have a solution for using hibernate sessions from quartz job class? 任何人都有使用石英作业类的hibernate会话的解决方案?

I know this is an old question, but I did a quick Google search, and this was the first hit. 我知道这是一个老问题,但我做了一个快速的谷歌搜索,这是第一次。

In the quartz job, add this line at the start of the method: 在quartz作业中,在方法的开头添加以下行:

public void execute(JobExecutionContext context) throws JobExecutionException
{
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //<-- this line

     //...your code here...
}

I apologize if this doesn't fix your specific issue, but I suspect it will catch someone in the future. 如果这不能解决您的具体问题我会道歉,但我怀疑它将来会抓住某人。

One approach is to use a HibernateUtil class which builds the SessionFactory in a static initializer and makes it available via a public static getter. 一种方法是使用HibernateUtil ,它在静态初始化程序中构建SessionFactory ,并通过public static getter使其可用。 Your Quartz job can create a Session as HibernateUtil.getSessionFactory().getCurrentSession() and use it. 您的Quartz作业可以创建一个Session作为HibernateUtil.getSessionFactory().getCurrentSession()并使用它。

Searching for "Quartz Hibernate" returned this. 搜索“Quartz Hibernate”返回了这个。 Coming to a different solution (and using Tapestry), I thought I'd share it. 来到一个不同的解决方案(并使用Tapestry),我想我会分享它。

when scheduling the Job: 在安排工作时:

…
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDataMap myJobDataMap = new JobDataMap();
myJobDataMap.put("HibernateSessionManager", hibernateSessionManager);
        myJobDataMap.put("PerthreadManager", perThreadManager);
JobDetail job = JobBuilder.newJob(SomeJob.class).withIdentity(
            "SomeJob", "someGroup").setJobData(
            myJobDataMap).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
            "Some Trigger", "someGroup").startNow().withSchedule(
            SimpleScheduleBuilder.repeatSecondlyForever(30)).build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
…

and in the Job 在工作中

public void execute(JobExecutionContext context)
                throws JobExecutionException
{
    JobDataMap jdm = context.getMergedJobDataMap();
    HibernateSessionManager hibernateSessionManager = (HibernateSessionManager) jdm.get("HibernateSessionManager");
    PerthreadManager perThreadManager = (PerthreadManager) jdm.get("PerthreadManager");

    Session session = hibernateSessionManager.getSession();
    //do stuff with session …
    //now clean up, otherwise I ended up with <IDLE> in transactions
    perThreadManager.cleanUp();
}

Hope somebody can use this. 希望有人可以使用它。

You can look at the below link to see if it gives you a direction to follow. 您可以查看以下链接,看看它是否为您提供了遵循的方向。 Since you are not using Spring, it might be hard to apply this directly 由于您没有使用Spring,因此可能很难直接应用它

http://forum.springsource.org/showthread.php?t=12117 http://forum.springsource.org/showthread.php?t=12117

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM