【问题标题】:How to access Hibernate session from src folder?如何从 src 文件夹访问 Hibernate 会话?
【发布时间】:2011-01-17 13:35:40
【问题描述】:

我想知道如何在 src/java 文件夹中的这个示例类中正确访问服务和域

public class NewsIngestion implements Runnable {

private String str;
private int num;
private Logger log = Logger.getLogger("grails.app");
private static boolean isRunning;
private Thread t;
private WorkerJobService jobService;
private NewsService newsService;

public NewsIngestion(String s, int n)
{
    jobService = new WorkerJobService();
    newsService = new NewsService();

    str = s;
    num = n;
    isRunning = false;

    t = new Thread (this, "NewsIngestion");

}

public void run ()
{

    while(isRunning){
        try{
            if(jobService.isJobEnabled("ConsumeFeedsJob") && jobService.lockJob("ConsumeFeedsJob")){
                log.info("${this.class.name}: ConsumeFeedsJob started");

                try{
                    // get all sources
                    List sources = (List) InvokerHelper.invokeMethod(RSSFeed.class, "list", null);

                    for(int i = 0; i < sources.size(); i++) {

                        RSSFeed s = (RSSFeed) sources.get(i);

                        // check if it's time to read the source
                        int diff = DateTimeUtil.getSecondsDateDiff(s.getLastChecked(), new Date());

                        if(s.getLastChecked() == null || diff >= s.getCheckInterval()){

                            List keyword_list = (List) InvokerHelper.invokeMethod(Keyword.class, "list", null);

                            for(int j = 0; j < keyword_list.size(); j++) {

                                String keyword = (String) keyword_list.get(j);

                                try{
                                    newsService.ingestNewsFromSources(keyword, s);
                                }catch(Exception e){
                                    log.error("${this.class.name}: ${e}");
                                }

                                log.debug("Completed reading feeds for ${keyword}.");
                                log.info("${this.class.name}: Reading feeds for '${keyword}' (${s.feedName}) took ${Float.toString(st2.getDuration())} second(s).");
                            }

                            s.setLastChecked(new Date());
                            InvokerHelper.invokeMethod(RSSFeed.class, "save", null);
                        }

                        log.info("${this.class.name}: Reading feeds for '${s.feedName}' for all keywords took ${Float.toString(st.getDuration())} second(s).");
                    }

                }catch(Exception e){
                    log.error("${this.class.name}: Exception: ${e}");
                }

                log.info("${this.class.name}: ConsumeFeedsJob ended.");

                // unlock job
                jobService.unlockJob("ConsumeFeedsJob");
            }

            log.info("alfred: success");

        }
        catch (Exception e){
            log.info("alfred exception: " + e.getMessage());
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            log.info(e.getMessage());
        }
    }
}

public void start() {
    if(t == null){
        t = new Thread (this, "NewsIngestion");
    }

    if(!isRunning){
        isRunning = true;
        t.start();
    }
}

public void stop() {
    isRunning = false;
}

public boolean isRunning() {
    return isRunning;
}

}

我遇到了这个错误信息:

没有绑定到线程的休眠会话, 并且配置不允许 在这里创建非事务性的

谢谢。

【问题讨论】:

    标签: java hibernate grails


    【解决方案1】:

    你不应该自己实例化服务类,而是从主上下文中获取类实例

    import org.codehaus.groovy.grails.commons.ApplicationHolder
    
    def ctx = ApplicationHolder.application.mainContext
    def newsService = ctx.newsService
    

    如果您使用的是 Java

    import org.codehaus.groovy.grails.commons.ApplicationHolder
    
    public class SomeClass  {
       SomeService someService;
       public SomeClass()  {
           someService = (SomeService) ApplicationHolder.getApplication().getMainContext().getBean("someService");
       }
    }
    

    【讨论】:

    • 感谢您的建议,但我不确定您是否理解我的问题。我的课程是 Java,而不是 Groovy。我可以将它转换到这里:ApplicationContext ctx = ApplicationHolder.getApplication().getMainContext();不知道接下来如何获取 newsService。
    • 如果你使用Java,那么你应该使用NewsService newsService = (NewsService) ApplicationHolder.getApplication().getMainContext().getBean("newsService");
    【解决方案2】:

    考虑使用 Spring 和 @Transactional 注解或 AOP。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多