• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java DAO object make multthread?  RSS feed

 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using struts1.3 action layer with hibernate. I am accessing through HeperFactory patten to create helper instance to access DAO layer.
The helper factory seems to be Singleton but thread safe. When i do multiple updates its taking long time.
Whan i want to achieve is there any design pattern or mechanism to achieve multi-threading helper access. Each and every thread(request) should have single helper instance per request for access dao.

I have two approaches
1. Using the singleton pattern (creating a single instance of each class and sharing them among threads)...
2. creating instances of the classes in each thread as and when they are needed.

How to achieve 2nd point? Is there any API or i should use pooling mechanism? Is there any issue in 2nd point(data loss)?

This is my existing code :
--------------------------

TaskAction.java
----------------
public class TaskAction{

public void udpateRecord(){
......
TaskHelper taskHelper = (TaskHelper) HelperFactory
.getHelperInstance(TaskHelper.class);
taskHelper.updateRecord(daoObj);
......
}
}

HelperFactory.java
------------------
public final class HelperFactory{

public static Object getHelperInstance(Class<?> helperClass) {
try {
return LookUpFactory.getLookUpFactory().lookUpClass(helperClass);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

LookUpFactory.java
-------------------
public final class LookUpFactory implements Serializable {
//SingleTon class
public static LookUpFactory getLookUpFactory() {
try {
if (factory == null) {
factory = new LookUpFactory();
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
return factory;
}

public Object lookUpClass(String lookupClass)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException {
if (lookupClass == null) {
throw new IllegalArgumentException(
"supplied lookupClass to lookup was null");
}
Object obj = this.objects.get(lookupClass);
if (obj == null) {
obj = Class.forName(lookupClass).newInstance();
this.objects.put(lookupClass, obj);
}
return obj;
}
}
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It's not clear to me what means in the LookUpFactory. Is it (objects) some kind of pool?

Besides that incognite,
you could use commons pool to maintain a pool of TaskHelper instances.
You have to activate and pasivate the instances from/to the pool explicitly in your code with the borrow and return methods,
and allow the pool to grow at least to the number of threads of your application/servlet container.


Cheers,
 
stshiva raaj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carles Gasques wrote:Hi,

It's not clear to me what means in the LookUpFactory. Is it (objects) some kind of pool?

Besides that incognite,
you could use commons pool to maintain a pool of TaskHelper instances.
You have to activate and pasivate the instances from/to the pool explicitly in your code with the borrow and return methods,
and allow the pool to grow at least to the number of threads of your application/servlet container.


Cheers,


Thanks for your reply. I pasted my current code. There is a pooling concept here. But this mechanism i could not achieve multi-threading concept. I need to create TaskHelper instance for each request. Right now this is singleton. I would love to create thread-based instance creation.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stshiva raaj wrote:There is a pooling concept here. But this mechanism i could not achieve multi-threading concept. I need to create TaskHelper instance for each request. Right now this is singleton. I would love to create thread-based instance creation.

What makes you think that this is the problem? Have you profiled it?

If this setup (which I've never used) actually uses pooling behind the scenes, then the business of TaskHelper being a singleton may be totally irrelevant.

Lesson: Don't choose a solution before you know (and furthermore, can PROVE) that it will, in fact, solve your problem.

Winston
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Are you sure that "Object obj = this.objects.get(lookupClass);" always return the same instance of TaskHelper?
If you are not sure, trace Thread id. and TaskHelper id. just after "objects.get" call.

If differents threads obtain the same TaskHelper instance you can create a new instance of TaskHelper at every "udpateRecord" method call, instead of using the HelperFactory.

Or you can explore the use of a pool of TaskHelpers avoiding the workload of create instances at every udpateRecord method call.


Best reggards,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!