Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Location
    Hangzhou PRC
    Posts
    1

    Smile Unanswered: JDBC Connection Thread Safty

    Hi everybody,

    I'm implementing a persistent framework with jdbc support, and once I'd decided to separate transaction enabled processing and query-only processing jobs, to improvement performence. And I intend to let all the query workers share as less connections as possible, in fact I'm planning to use only one connection for all query workers in the very beginning version of the framework. But here comes an thread safty issue, that's if I create numbers of Statements throught a single Connection, and execute them each in a separate thread, It might be troubsome. I went over the JDBC3.0 specification, but it doesn't mention thread safty at all. So has here anyone met simular situations already? Or any idea is appreciated.

    Appreciating

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1

    Re: JDBC Connection Thread Safty

    Originally posted by complystill
    Hi everybody,

    I'm implementing a persistent framework with jdbc support, and once I'd decided to separate transaction enabled processing and query-only processing jobs, to improvement performence. And I intend to let all the query workers share as less connections as possible, in fact I'm planning to use only one connection for all query workers in the very beginning version of the framework. But here comes an thread safty issue, that's if I create numbers of Statements throught a single Connection, and execute them each in a separate thread, It might be troubsome. I went over the JDBC3.0 specification, but it doesn't mention thread safty at all. So has here anyone met simular situations already? Or any idea is appreciated.

    Appreciating
    Statements and ResultSets are not thread-save, obviously, so you'll have to control access to them yourself. As long as any single thread does not attempt to do anything to the resources that belong to another thread you should be ok.

    There's one problem though - you only release resources when you commit, and commit() is Connection-wide; whenever a thread issues commit this will release all resources that belong to the same connection. You'll see all sorts of side effects due to that.

    I'd suggest you use a separate Connection for each thread; that'll be more consistent and safe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •