Home  >  Q&A  >  body text

java - 多线程ThreadLocal相关求助

当前项目的connection都是放在ThreadLocal里面的。由于我的某个功能操作有部分打算异步处理,new Thread(()->{//.......}).start() 这样就导致无法使用dao了。因为dao需要的connection在ThreadLocal中。

黄舟黄舟2765 days ago763

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-04-18 09:53:54

    The connection must be released after each use. The next time you connect, just go to threadlocal to get the value. I don’t understand what you mean.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:53:54

    When creating a new Thread, take the connection out of ThreadLocal and pass it in

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:53:54

    Isn’t there a way to get the connection?

    reply
    0
  • 阿神

    阿神2017-04-18 09:53:54

    Just write the execution part independently, so you don’t need to pass the dao to Thread

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:53:54

    If this asynchronous operation is non-transaction related to the previous logic. That is to say, they are not in the same transaction.
    Then you can use a new link.

    If there is a transaction, this old link must be used. This connection object can only be passed to the thread.

    reply
    0
  • Cancelreply