java - Is GenericObjectPools borrowObject Method thread safe? -
in question is genericobjectpool<t> commons.apache.org thread safe? mentioned thread safe .
edited:but im having situation in multithreaded application 2 threads getting same object pool @ same time.-this statement wrong.
i moved borrowobject synchronize block , solved issue.
has faced issue earlier?
here code:
public static genericobjectpool<idocbuilderpool> documentbuilderpool = new genericobjectpool(new documentpool()); static { documentbuilderpool.setmaxactive(1000); documentbuilderpool.setmaxwait(30000); documentbuilderpool.setmaxidle(-1); } //method returns document pool called multiple threads . public static idocbuilderpool getdocumentpool() { return documentbuilderpool.borrowobject(); } //the pool factory class public class documentpool extends basepoolableobjectfactory<icollabrrdocument> { public domdocumentpool() { } @override public domdocument makeobject() throws exception { // todo auto-generated method stub return new domdocument(); } @override public void activateobject(idocbuilderpool obj) throws exception { // todo auto-generated method stub super.activateobject(obj); } @override public void destroyobject(idocbuilderpool obj) throws exception { // todo auto-generated method stub super.destroyobject(obj); } @override public void passivateobject(idocbuilderpool obj) throws exception { // todo auto-generated method stub obj.release(); super.passivateobject(obj); } @override public boolean validateobject(idocbuilderpool obj) { // todo auto-generated method stub return super.validateobject(obj); } } public class domdocument implements idocbuilderpool { private document domdocument; private documentbuilder documentbuilder; private documentbuilderfactory documentbuilderfactory; public hashmap<org.w3c.dom.node, domelement> elementmap = new hashmap<org.w3c.dom.node, domelement>(); public long threadid; public domdocument() { setdomdocument(); this.threadid = thread.currentthread().getid(); } public void setdomdocument() throws this.documentbuilderfactory = documentbuilderfactory.newinstance(); this.documentbuilderfactory.setnamespaceaware(true); this.documentbuilder = this.documentbuilderfactory.newdocumentbuilder(); this.domdocument = this.documentbuilder.parse(new bytearrayinputstream("<root/>".getbytes())); } }
the documentation of poolableobjectfactory states:
poolableobjectfactory must thread-safe.
looking @ code, thing thread unsafe call obj.release();. possibly problem is.
apart looks ok...
Comments
Post a Comment