File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes ThreadLocal and DB connection... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadLocal and DB connection..." Watch "ThreadLocal and DB connection..." New topic

ThreadLocal and DB connection...

garalo garalo

Joined: Jul 13, 2006
Posts: 3

I am connecting Database with this class but I want to use LocalThread. How can I do it? Do I need another ThreadLocal class or should I edit this MainDao class? This is MainDAO class and I have got another class also. My project is inside of the MVC and I used only servlet not framework nor anything. My code is here:

public class MainDAO {

public MainDAO() {
conn = makeConn();
//conn = new ThreadLocal();

static Connection conn = null;
// public static Connection getConnection() {
// return (Connection) conn.get();
// }

// Statement statement;

public Connection makeConn() {
// private static ThreadLocal conn = new ThreadLocal();
// public Object initialValue() {

try {

DaoProperties dp = DaoProperties.getInstance();

String driver = dp.getProperty("swallow.jdbc.driver");
String url = dp.getProperty("swallow.jdbc.url");
String user = dp.getProperty("swallow.jdbc.user");
String password = dp.getProperty("swallow.jdbc.password");

return DriverManager.getConnection(url, user, password);

} catch (Exception e) {
return null;

protected void finalize() {
if (conn != null) {
try {
} catch (SQLException e) {

public void rollback() throws SQLException {
if (conn != null) {

public void commit() throws SQLException {
if (conn != null) {

I comment out some parts because I am not sure how I use. Please help me...
Justin Yao

Joined: Jun 16, 2006
Posts: 19
Some suggestion:
You can consider ThreadLocal as a HashMap, you can store Objects into a HashMap object
like this: map.put("Key", "Value"); , and the usage of ThreadLocal is:
threadLocal.set("value"); , but what is the key? It is Thread.currentThread().
So you can store your database conncetion like this:
ThreadLocal threadLocal = new ThreadLocal();
and get a conncetion like this:

You'd better not to overwrite the finalize method, because we cannot control the method.
you should release the connection like this:
public void release() {

I guess you want to get the connection in a conversation, for this you can store your
database connection in a session object, because you can get a session object in your

Hope this is useful to you.
garalo garalo

Joined: Jul 13, 2006
Posts: 3
Thank you very much.
I haven`t understod some points. Where will I write "HashMap object",
and where is my "initialValue"?. plase give a simple example?. I have been thinking on it since one week. Please help...
Henry Wong

Joined: Sep 28, 2004
Posts: 18509

A thread local object is simply a storage object that has a setter and a getter. When you call the setter with an object, it will return it back when you call the getter later.

The feature that makes it "thread local" is that each thread is treated in isolation. Two different threads can call the setter, and each will get their respective objects back when they each call the getter. They can share the same thread local object, and not interfere with each other -- they also can't really "pass objects" with each other either.

As for the initalValue() method, it is a method that can be called by the getter, if the setter is never called. This method is only needed for times when there can be a mechanism that can initially define (calculate) the value for all the threads.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
garalo garalo

Joined: Jul 13, 2006
Posts: 3
Thank you very much. I will try it on my project. I hope, I can be success on it.
I agree. Here's the link:
subject: ThreadLocal and DB connection...
Similar Threads
Newbie having problems with StrutsMocktestCase
jsf and database connection problem
Connection with database Help