This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadLocal and DB connection..." Watch "ThreadLocal and DB connection..." New topic
Author

ThreadLocal and DB connection...

garalo garalo
Greenhorn

Joined: Jul 13, 2006
Posts: 3
Hi,

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");

Class.forName(driver);
return DriverManager.getConnection(url, user, password);

} catch (Exception e) {
return null;
}
}


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

public void rollback() throws SQLException {
if (conn != null) {
conn.rollback();
conn.close();
}
}

public void commit() throws SQLException {
if (conn != null) {
conn.commit();
conn.close();
}
}
}

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

Joined: Jun 16, 2006
Posts: 19
Hello,
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();
threadLocal.set(conn);
and get a conncetion like this:
threadLocal.get();

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() {
conn.close();
}

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
servlet.

Hope this is useful to you.
garalo garalo
Greenhorn

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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

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.

Henry


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

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: http://aspose.com/file-tools
 
subject: ThreadLocal and DB connection...
 
Similar Threads
BeanInstantiationException
jsf and database connection problem
nullpointerexception
Newbie having problems with StrutsMocktestCase
Connection with database Help