在多线程应用程序中解决Hibernate的错误 [英] Work around Hibernate's errors in multithreaded applications

查看:128
本文介绍了在多线程应用程序中解决Hibernate的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我们在尝试在多线程应用程序中使用Hibernate时遇到的所有问题(第一条线索第二条线索第三条线索等),我正在考虑另一种解决方案:在经典 控制器中实现逻辑部分,并简单地使用 URL从我的线程调用它.openConnection()



换句话说,不要这样做:

MyThread.java

  public class MyThread implements Runnable {

@Override
public void run(){
//用Hibernate做一些很棒的事情
}

}

$ p $ Anywhere $ java Anywhere.java





$ b

  new Thread(new MyThread())。start(); 

我想尝试这样的事情:



MyController.java

  @Controller 
public class MyController {

@RequestMapping(value =myUrl,method = RequestMethod.GET)
public void myMethod(){
//用Hibernate做一些很棒的事情

$ b}

MyThread.java

  public class MyThread implements Runnable {
$ b $ @Override
public void run(){
//简单地调用上面映射的url
}

}

Anywhere.java

  new Thread(new MyThread())。start(); 

您怎么看?是好是坏?我还没有尝试过,但我认为这样的解决方案将防止我们在多线程中使用Hibernate可能遇到的常见错误,因为服务器将执行逻辑部分,就好像有人要求伪造的页面一样。

PS:我知道在多线程应用程序中使用Hibernate有一些解决方案 ,但每次我尝试一次,另一次出现,

我知道这样的解决方案需要(例如UID作为标记)。

解决方案

我真的不知道你想解决什么问题这里。 Hibernate几乎总是用在多线程环境中。例如,在webapps中,并发请求由多个并发线程处理,每个线程使用自己的Hibernate会话。这不会导致任何问题。



如果您在线程之间共享相同的会话,或者如果您在线程中共享给定的实体,则会遇到问题。



如果您启动自己的线程,并且此线程使用自己的会话和实体,我不明白为什么会有任何问题。


Because of all the problems we can meet when trying to use Hibernate in a multithreaded application (1st clue, 2nd clue, 3rd clue, etc.), I was thinking of another solution: implementing the logical part within a classic Controller, and simply call it from my thread using URL.openConnection().

In other words, instead of doing something like this:

MyThread.java

public class MyThread implements Runnable {

    @Override
    public void run() {
        // do some great stuff with Hibernate
    }

}

Anywhere.java

new Thread(new MyThread()).start();

I would like to try something like that:

MyController.java

@Controller
public class MyController {

    @RequestMapping(value = "myUrl", method = RequestMethod.GET)
    public void myMethod() {
        // do some great stuff with Hibernate
    }

}

MyThread.java

public class MyThread implements Runnable {

    @Override
    public void run() {
        // simple call the above mapped url
    }

}

Anywhere.java

new Thread(new MyThread()).start();

What do you think about it? Good or bad? I haven't tried yet, but I think such a solution will prevent the common errors we can meet using Hibernate in multithreading, because the server will execute the logical part as if someone were requesting the fake page.

PS: I know there are some solutions to use Hibernate in multithreaded applications, but each time I try one, another appears, and that until the I'm-fed-up-with-it point of no return.

PS2: I'm aware that such a solution need to be secured (e.g. UID as a token).

解决方案

I don't really see what problem you're trying to solve here. Hibernate is almost always used in a multi-threaded environment. In webapps, for example, concurrent requests are handled by multiple concurrent threads, and each thread uses its own Hibernate session. And that doesn't cause any problem.

You will have problem if you share the same session among threads, or if you share a given entity among threads.

If you start your own thread, and this thread uses its own session and entities, I don't see why you would have any problem.

这篇关于在多线程应用程序中解决Hibernate的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆