pajarokillo
Programmer
Hi, i have a factory class that initialize a connection pool, and i would like to known if is a good idea to create that factory class in the my PlugIn class and to put it in then context application and then i can use it in my action classes, for example
My factory class
public class MyFactoryImpl implements IMyFactory{
public MyFactoryImpl(){
Context initCtx = null;
//lookup my dataSource
initCtx = new InitialContext();
DataSource myDataSource = (DataSource) initCtx.lookup(dsName);
//initialize my connection pool
Pool.init(myDataSource);
}
public ICustomer createCustomer(){
Customer customer = new Customer();
}
}
My PlugIn class
public class MyPlugIn implements PlugIn {
public void init(ActionServlet servlet, ModuleConfig config){
//get the factory class
IMyFactory myFactory= new MyFactoryImpl();
//set the myFactory objetc in the context application
servlet.getServletContext().setAttribute(IConstants.MyFactory,myFactory);
}
}
and my client class
public class Client extends Action{
public execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception{
IMyFactory myFactory = (IMyFactory) getApplicationObject(IConstants.MyFactory);
Customer customer = myFactory.createCustomer();
}
}
It could generate something conflict between users that acces to the factory class?
My factory class
public class MyFactoryImpl implements IMyFactory{
public MyFactoryImpl(){
Context initCtx = null;
//lookup my dataSource
initCtx = new InitialContext();
DataSource myDataSource = (DataSource) initCtx.lookup(dsName);
//initialize my connection pool
Pool.init(myDataSource);
}
public ICustomer createCustomer(){
Customer customer = new Customer();
}
}
My PlugIn class
public class MyPlugIn implements PlugIn {
public void init(ActionServlet servlet, ModuleConfig config){
//get the factory class
IMyFactory myFactory= new MyFactoryImpl();
//set the myFactory objetc in the context application
servlet.getServletContext().setAttribute(IConstants.MyFactory,myFactory);
}
}
and my client class
public class Client extends Action{
public execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception{
IMyFactory myFactory = (IMyFactory) getApplicationObject(IConstants.MyFactory);
Customer customer = myFactory.createCustomer();
}
}
It could generate something conflict between users that acces to the factory class?