Reputation: 73
The create method of PooledObjectFactory has no parameter
If my Foo class definition is:
public class Foo {
private String name;
public Foo(String name) {
super();
this.name = name;
}
}
Can this Foo can be pooled by commons-pool ?
Thank you for any advice
Upvotes: 2
Views: 785
Reputation: 601
Because objects cannot be created by abstract classes, you need to extend BasePooledObjectFactory and implement its abstract methods. By doing this, you can create your own class(for example, FooFactory) which contains a constructor with parameters. After that, you can use your own class to instantiate objects(namely Foo).
Sample Code:
public class FooFactory extends BasePooledObjectFactory<Foo> {
private String name;
public FooFactory(String name) {
this.name = name;
}
@Override
public Foo create() throws Exception {
return new Foo(name);
}
}
Upvotes: 1