• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a connection to a child thread..

 
Brian Quinn
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to spawn a thread that will require the database connection that the parent thread has. How do I get the child thread to gain that property. I tried InheritableThreadLocal - but I didn't seem to bring the connection over to the child thread.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you extend Thread or implement Runnable to write your own class to run on the worker thread? You can pass that object anything you like in the constructor or via set. Something like ...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I understand about ThreadLocal, it is used to make a unique instance of a class on a per-thread basis, so it is the opposite of what you want. I usually use queues or pipes to move data between threads. Depending on your setup you could also consider shared variables or just passing it in a setter/constructor. I'd probably use a connection pool, but you seem sold on using the same connection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!