我正在使用带有hibernate的struts1.3动作层。我正在通过 HeperFactory 模式访问以创建帮助程序实例来访问 DAO 层。助手工厂似乎是单例但线程安全。当我进行多次更新时,它需要很长时间。我想要实现的是有任何设计模式或机制来实现多线程助手访问。每个线程(请求)都应该有每个请求访问 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.
如何达到第二点?是否有任何 API 或者我应该使用池机制?第二点(数据丢失)有什么问题吗?
这是我现有的代码:
任务动作.java
公共类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 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;
}