Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ThreadLocal and DB connection...

 
garalo garalo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
garalo garalo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I will try it on my project. I hope, I can be success on it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic