Writing a ConnectionProvider for Quartz Scheduler

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing a ConnectionProvider for Quartz Scheduler

Nicolai Willems
Hello there.

I'm interested in writing a ConnectionProvider implementation for
Quartz Scheduler, and I would like to have MyBatis handle my
connections.
So I thought - hey I can just use the Session objects getConnection
method, right?

But reading through the Quartz source - I found that they actually
close the connection, not by using the ConnectionProviders method
shutdown() - but by calling "connection.close()".
So my question is: how will this have effect on MyBatis?

Maybe when my "investigation" is done - I could write an article on
the wiki about using MyBatis for external libraries(which needs a
connection).

Regards Nicolai
Reply | Threaded
Open this post in threaded view
|

Re: Writing a ConnectionProvider for Quartz Scheduler

Simone Tripodi-2
Hi Nicolai,
interesting thoughts, please post your research results so the whole
community can participate on your activity.
All the best and good luck,
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Mon, Jan 24, 2011 at 12:25 AM, Nicolai Willems <[hidden email]> wrote:

> Hello there.
>
> I'm interested in writing a ConnectionProvider implementation for
> Quartz Scheduler, and I would like to have MyBatis handle my
> connections.
> So I thought - hey I can just use the Session objects getConnection
> method, right?
>
> But reading through the Quartz source - I found that they actually
> close the connection, not by using the ConnectionProviders method
> shutdown() - but by calling "connection.close()".
> So my question is: how will this have effect on MyBatis?
>
> Maybe when my "investigation" is done - I could write an article on
> the wiki about using MyBatis for external libraries(which needs a
> connection).
>
> Regards Nicolai
Reply | Threaded
Open this post in threaded view
|

Re: Writing a ConnectionProvider for Quartz Scheduler

Eduardo Macarron
In reply to this post by Nicolai Willems
That should work. More or less that is the same situation than for
Spring Framework.

You can code your own Transaction for MyBatis, as we did or maybe you
can just use the External/Managed transaction that comes out of the
box that wll let you pass a property so it will not close the
connection (otherwise you will try to do it two times).

Post your progress/problems/questions here so we can help :)

On 24 ene, 00:25, Nicolai Willems <[hidden email]> wrote:

> Hello there.
>
> I'm interested in writing a ConnectionProvider implementation for
> Quartz Scheduler, and I would like to have MyBatis handle my
> connections.
> So I thought - hey I can just use the Session objects getConnection
> method, right?
>
> But reading through the Quartz source - I found that they actually
> close the connection, not by using the ConnectionProviders method
> shutdown() - but by calling "connection.close()".
> So my question is: how will this have effect on MyBatis?
>
> Maybe when my "investigation" is done - I could write an article on
> the wiki about using MyBatis for external libraries(which needs a
> connection).
>
> Regards Nicolai
Reply | Threaded
Open this post in threaded view
|

Re: Writing a ConnectionProvider for Quartz Scheduler

Nicolai Willems
So I used the day to experiment on this.

Using a pooled data source - and just "handing" off a connection to
Quartz is kinda okay. Since it never gives it back to the pool, it
causes no issues while running.
BUT - when the scheduler is told to shutdown it closes the connection,
and the next SqlSession with that connection assigned, fails with a
message like:
    "... Cause: org.apache.ibatis.datasource.DataSourceException:
Error accessing PooledConnection. Connection is invalid."

So in my scenario, where the scheduler will be running from start to
end of application, that shouldn't mess up anything - except my head,
which still nows that this is dirty!

Maybe configuring Quartz for a managed connection, could solve its
need for closing and cleaning.

On Jan 24, 10:06 am, Eduardo <[hidden email]> wrote:
> That should work. More or less that is the same situation than for
> Spring Framework.
>
> You can code your own Transaction for MyBatis, as we did or maybe you
> can just use the External/Managed transaction that comes out of the
> box that wll let you pass a property so it will not close the
> connection (otherwise you will try to do it two times).

Problem is not MyBatis - more if anyone tried using MyBatis to support
Quartz for its JDBC part
And that Quartz is closing things that it shouldn't.

>
> Post your progress/problems/questions here so we can help :)

So far my progress is 300 lines of dirty dirty code doing scheduling
and threading to cause all sort of bad luck.
The code basically does:
1. Init MyBatis
2. Start 10 threads inserting into the database
3. Create and start the scheduler
4. If it doesn't contain any jobs - create 4 jobs to run  at 0, 15,
30, 45 second of every minute
5. Open up a thread containing only a JOptionPane, and make a while
loop waiting for content to come out of the JOptionPane
6. Shutdown scheduler and shutdown the 10 threads from step 2.

Thats about it. I'm ashamed. If you would like to see some log output
from Quartz and MyBatis, I guess it can be posted, but theres quite a
lot.

>
> On 24 ene, 00:25, Nicolai Willems <[hidden email]> wrote:
>
>
>
>
>
>
>
> > Hello there.
>
> > I'm interested in writing a ConnectionProvider implementation for
> > Quartz Scheduler, and I would like to have MyBatis handle my
> > connections.
> > So I thought - hey I can just use the Session objects getConnection
> > method, right?
>
> > But reading through the Quartz source - I found that they actually
> > close the connection, not by using the ConnectionProviders method
> > shutdown() - but by calling "connection.close()".
> > So my question is: how will this have effect on MyBatis?
>
> > Maybe when my "investigation" is done - I could write an article on
> > the wiki about using MyBatis for external libraries(which needs a
> > connection).
>
> > Regards Nicolai
Reply | Threaded
Open this post in threaded view
|

Re: Writing a ConnectionProvider for Quartz Scheduler

Nicolai Willems
I don't know if its considered a bad smell, but I found that the only
thing I should prohibit Quartz from doing is closing the connection.
So I found that making a "NonClosingProxyConnection" would be a
solution. Any thoughts on this?

The behaviour for it is:
If a method related to close is called do the operation on a boolean
instance variable.
F.ex. close() would set the isClosed variable to true; and then
calling isClosed() would give "correct" results.

The rest of the methods in the class - just forwards the call to its
internally stored connection object.

/Nicolai Willems

On Jan 24, 3:47 pm, Nicolai Willems <[hidden email]> wrote:

> So I used the day to experiment on this.
>
> Using a pooled data source - and just "handing" off a connection to
> Quartz is kinda okay. Since it never gives it back to the pool, it
> causes no issues while running.
> BUT - when the scheduler is told to shutdown it closes the connection,
> and the next SqlSession with that connection assigned, fails with a
> message like:
>     "... Cause: org.apache.ibatis.datasource.DataSourceException:
> Error accessing PooledConnection. Connection is invalid."
>
> So in my scenario, where the scheduler will be running from start to
> end of application, that shouldn't mess up anything - except my head,
> which still nows that this is dirty!
>
> Maybe configuring Quartz for a managed connection, could solve its
> need for closing and cleaning.
>
> On Jan 24, 10:06 am, Eduardo <[hidden email]> wrote:
>
> > That should work. More or less that is the same situation than for
> > Spring Framework.
>
> > You can code your own Transaction for MyBatis, as we did or maybe you
> > can just use the External/Managed transaction that comes out of the
> > box that wll let you pass a property so it will not close the
> > connection (otherwise you will try to do it two times).
>
> Problem is not MyBatis - more if anyone tried using MyBatis to support
> Quartz for its JDBC part
> And that Quartz is closing things that it shouldn't.
>
>
>
> > Post your progress/problems/questions here so we can help :)
>
> So far my progress is 300 lines of dirty dirty code doing scheduling
> and threading to cause all sort of bad luck.
> The code basically does:
> 1. Init MyBatis
> 2. Start 10 threads inserting into the database
> 3. Create and start the scheduler
> 4. If it doesn't contain any jobs - create 4 jobs to run  at 0, 15,
> 30, 45 second of every minute
> 5. Open up a thread containing only a JOptionPane, and make a while
> loop waiting for content to come out of the JOptionPane
> 6. Shutdown scheduler and shutdown the 10 threads from step 2.
>
> Thats about it. I'm ashamed. If you would like to see some log output
> from Quartz and MyBatis, I guess it can be posted, but theres quite a
> lot.
>
>
>
>
>
>
>
>
>
> > On 24 ene, 00:25, Nicolai Willems <[hidden email]> wrote:
>
> > > Hello there.
>
> > > I'm interested in writing a ConnectionProvider implementation for
> > > Quartz Scheduler, and I would like to have MyBatis handle my
> > > connections.
> > > So I thought - hey I can just use the Session objects getConnection
> > > method, right?
>
> > > But reading through the Quartz source - I found that they actually
> > > close the connection, not by using the ConnectionProviders method
> > > shutdown() - but by calling "connection.close()".
> > > So my question is: how will this have effect on MyBatis?
>
> > > Maybe when my "investigation" is done - I could write an article on
> > > the wiki about using MyBatis for external libraries(which needs a
> > > connection).
>
> > > Regards Nicolai