From: Adam R. <ad...@ex...> - 2011-05-31 20:21:58
|
> I've figured out why I'm getting that Error, it's because I'm creating a new > broker by instantiating a new NativeBroker, rather than using the > BrokerPool.get(user) function, which means the broker is never being > registered with the BrokerPool. Exactly. A pattern I am using for code that needs to take action on the database looks like this - private interface BrokerOperation { public void withBroker(DBBroker broker) throws EXistException, URISyntaxException, PermissionDeniedException; } private void executeWithBroker(BrokerOperation brokerOperation) throws EXistException, URISyntaxException, PermissionDeniedException { DBBroker broker = null; try { broker = factory.getBrokerPool().get(user); brokerOperation.withBroker(broker); } finally { if(broker != null) { factory.getBrokerPool().release(broker); } } } > I've been doing it this way because if I use > BrokerPool.get(user) and the user is someone other than the current user, it > changes for the rest of my execution. I realize I can set it back, but I > have functions where I don't want them tied to requiring a context be passed > in. Erm, im not really clear on why you have this problem? or why it is a problem? Perhaps you are holding onto a broker for too long, i.e. performing lots of operations with a single broker, sometimes this makes sense, but if you have distinct code units then sometimes it is better to treat a broker as a hot-potato and just grab it when you absolutely have to, and release it as soon as possible. The above pattern works well in this scenario, and if you do want to switch user, the pattern could be extended to accomodate that with a executeWithBrokerAsUser(BrokerOperation, User user) function. > Now that I think about it, this may not be an issue in the trunk (I think I > recall seeing some broker.setUser(user) commented out in finally > statements). Maybe this is an easy port back to 1.4.1? I'd be happy to do it > if so... Could you be more exact? > > Cheers, > > Patrick > > On Tue, May 31, 2011 at 2:43 PM, Patrick Bosek <pat...@jo...> > wrote: >> >> Follow up question... Do I need to release or destroy the contexts I >> create somehow? >> >> On Tue, May 31, 2011 at 2:34 PM, Patrick Bosek <pat...@jo...> >> wrote: >>> >>> Hi everyone, >>> >>> I'm trying to figure out when I should and should not release a broker, >>> as it seems to be critical to eXist running well. >>> >>> So, right now I'm getting these is my exist.log: >>> 2011-05-31 13:21:32,314 [P1-8] ERROR (BrokerPool.java [release]:1312) - >>> release() has been called from the wrong thread for broker null >>> >>> Which I assume is because I'm releasing a broker when I shouldn't. These >>> are appearing most notably after creating a new broker and a new context, >>> then releasing the broker. Like this: >>> >>> class myUtil { >>> >>> public static XQueryContext getNewContextAsUser(String username){ >>> >>> NativeBroker broker = getBrokerAsUser(username); >>> >>> XQueryContext newContext = new XQueryContext(broker, >>> AccessContext.XMLRPC); >>> >>> return newContext; >>> } >>> >>> public static NativeBroker getBrokerAsUser(String username) >>> { >>> BrokerPool pool; >>> NativeBroker broker = null; >>> try { >>> pool = BrokerPool.getInstance(); >>> >>> org.exist.security.SecurityManager security = >>> pool.getSecurityManager(); >>> User asUser = security.getUser(username); >>> >>> broker = new NativeBroker(pool, pool.getConfiguration()); >>> >>> broker.setUser(asUser); >>> >>> } catch (EXistException e) { >>> System.out.println("Exception in getBrokerAsUser: " + e); >>> e.printStackTrace(); >>> >>> return null; >>> } >>> >>> return broker; >>> } >>> >>> public static void brokerReleaseWrapper(DBBroker broker) >>> { >>> StackTraceElement[] trace = new Throwable().getStackTrace(); >>> >>> existLOG.info("brokerReleaseWrapper being called"); >>> for(int i = 0; i < 4; i++) >>> existLOG.info(trace[i]); >>> >>> broker.getBrokerPool().release(broker); >>> } >>> >>> } >>> >>> // Code that's causing the error >>> >>> XQueryContext context = >>> myUtil.getNewContextAsUser(org.exist.security.SecurityManager.DBA_USER); >>> >>> NativeBroker broker = (NativeBroker) context.getBroker(); >>> >>> try{ >>> >>> *** DO STUFF **** >>> } finally{ >>> broker.setUser(currentUser); >>> myUtil.brokerReleaseWrapper(broker); >>> } >>> >>> >>> >>> In general, when do I need to be sure I release a broker? >>> >>> >>> Does the XQueryContext release it's broker automatically somehow? >>> >>> >>> >>> Thanks! >>> >>> >>> -- >>> Patrick Bosek >>> Jorsek Software >>> Cell (585) 820 9634 >>> Office (585) 239 6060 >>> Jorsek.com >>> >> >> >> >> -- >> Patrick Bosek >> Jorsek Software >> Cell (585) 820 9634 >> Office (585) 239 6060 >> Jorsek.com >> > > > > -- > Patrick Bosek > Jorsek Software > Cell (585) 820 9634 > Office (585) 239 6060 > Jorsek.com > > > ------------------------------------------------------------------------------ > Simplify data backup and recovery for your virtual environment with vRanger. > Installation's a snap, and flexible recovery options mean your data is safe, > secure and there when you need it. Data protection magic? > Nope - It's vRanger. Get your free trial download today. > http://p.sf.net/sfu/quest-sfdev2dev > _______________________________________________ > Exist-open mailing list > Exi...@li... > https://lists.sourceforge.net/lists/listinfo/exist-open > > -- Adam Retter eXist Developer { United Kingdom } ad...@ex... irc://irc.freenode.net/existdb |