----- Original Message -----
From: "Mario Ivankovits" <[EMAIL PROTECTED]>
To: "Jakarta Commons Developers List" <[EMAIL PROTECTED]>
Sent: Monday, March 24, 2003 8:57 PM
Subject: Re: [DBCP] Please let us start to improve the connection pool
> 1) ThreadLocal
> This might not help anything, or do i oversee something?
>
> Thread A gets a connection from the pool
> Thread A loops until the abandoned connection interval is passed
> The connection will be passed back to the pool.
> Thread B gets a connection from the pool (the connection which was passed
> back automatically by some pool mechanism)
> Thread B starts a transaction
> Thread A wakes up from sleep and call "commit()"
> Thread B roll back transaction
>
I use thread locals this way in web applications :
MyServlet extends SomeServlet{
void service(Request request, Response reslonse)throws
ServletExeption,IOException{
Connection lazyConnctionWrapper = decorate(getConection());
ThreadLocalConnection.set(lazyConnctionWrapper);
try{
super.service(request, response);
lazyConnctionWrapper.commit()//IfOpen();
}catch( Throwable t ){
lazyConnctionWrapper.rollback();//IfOpen();
}finally{
lazyConnctionWrapper.close();//IfOpen();
ThreadLocalConnection.set(null);//not very usefull
}
}
}
class ThreadLocalConnection{
static ThreadLocal connections = new ThreadLocal();
static Connection get(){
Connection result = connections.get();
if(result == null ){
throw IllegalStateExeption("no connection for current thread');
}else {
return result;
}
}
static void set(Connection connection){
connections.set(connection);
}
ThreadLocalConnection.get() is used to get connection in application,
"lazyConnctionWrapper.close();//IfOpen();" is single place in application (s
ingle servlet) I need to close connection;
The same idea I use in JMS applications (single message listener/dispatcher
per application)
> Well, Thread A and B do always have their own TheadLocal and so, the
Thread
> do not "know", that the connection is abandoned.
>
> 2) Proxy
> Your proxy looks much more interesting, but i think, it does also not
> correctly handle the problem described above. The proxy do not "change"
the
> thread, only the connection does. For sure, i do not have much experiences
> with the java "Proxy" thinggy, but why should
> if( owner.get() == Thread.currentThread() )
> ever by "not true", the thread is still alive.
> But maybe we could catch the method "passivate" and set the "connection"
to
> null, now the connection is really released.
> I have to try this.
>
> I my case, i can easy implement such method. We have only one central
place
> where we ge a connection from the pool, but i suggest this might not be
> correct for all sort of application out there. Such an application then
has
> problems to decorate the connection instance.
>
> At least, i do not understand why this should be more elegant, if it works
> EVERY application has to read the docs, and implement this. This is like
> "cut and paste" in an OO design, isnt it?
>
> Mario
>
> > class MySafeConnection implements InvokationHandler{
> >
> > Connection connection;
> > WeakReference owner;
> >
> > MySafeConnection(Connection connection){
> > this.connection = connection;
> > owner = new WeakReference( Thread.currentThread() );
> > }
> >
> > Object invoke(Object obj, Method method, Object args[] ) throw
Throwable{
> >
> > if( owner.get() == Thread.currentThread() ){
> > method.invoke( connection, args );
> > }else{
> > throw new IllegalStateExeption();
> > }
> >
> > }
> >
> > static Connection decorate(Connection connection){
> >
> > return
> >
(Connection)Proxy.newProxyInstance(connection.getClass().getClassLoader(),
> > new Class[] {Connection.class}, new
> > MySafeConnection(connection));
> >
> > }
> >
> > }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]