Thanks,
Oh, that other thread you linked to was my question :). I didn't see the
post go through.
I do several statements within a transaction sometimes, so really need
the same connection for a number of statements.
Closing the factory was more meant to be aimed at making sure that there
isn't an illegal reference to a connection which could be being used
elsewhere.
I guess it really makes more sense to close the underlying connection
rather than the factory. (Probably a bad idea to re-use connections
without some kind of pooling to return them to some consistent state
anyway!)
Thanks!
Ryan
On 9/07/2012 3:51 PM, Lukas Eder wrote:
Hi Ryan,
This was recently discussed here:
https://groups.google.com/d/topic/jooq-user/xi2NQva0H1Y/discussion
Is this a good way to do it? Is it expensive to create them? Or should I be
pooling the factories too?
No it isn't expensive to create them. They are essentially just
wrappers for a JDBC Connection/DataSource. They should have no
relevant initialisation overhead.
Also, kinda related... is there a way to close a factory so that updateable
records don't accidently try and use the factories connection when it has
actually been reassigned to somewhere else (from the connection pool). So if
I call MyRecord.store() and it was attached to a closed factory, it will
give an exception.
That is a good question. I wonder if this kind of lifecycle can be
handled when you supply a JDBC Connection to the Factory. I assume
that using DataSource in the Factory constructor (available since jOOQ
2.4) is the better way to go. This was described in the most recent
release notes:
http://www.jooq.org/notes.php#2.4.0
Essentially, jOOQ fetches a JDBC Connection from the DataSource when
you execute the query, and returns (closes) it again, when the query
execution is finished. The returned record will then hold a reference
to the DataSource. When you call store(), it will fetch a new
Connection from it. That way, you should not end up with "stale"
records as you do today. More info on jOOQ's support for DataSources
can be seen here:
https://groups.google.com/d/topic/jooq-user/eiwxsnVPWV0/discussion
If you cannot use a JDBC DataSource, then you have to handle the
Connection lifecycle yourself. One option is to manually "re-attach"
the "stale" UpdatableRecords to a correct Factory using
http://www.jooq.org/javadoc/latest/org/jooq/impl/Factory.html#attach(org.jooq.Attachable...)
This is also documented in the manual:
http://www.jooq.org/manual/JOOQ/Serializability/
Another option is to handle this in an ExecuteListener and supply the
right Connection to jOOQ in the start() method:
http://www.jooq.org/javadoc/latest/org/jooq/ExecuteListener.html#start(org.jooq.ExecuteContext)
2012/7/9 Ryan How<[email protected]>:
Hi,
Is there a best practice for creating factories?.
I am pretty much creating 1 factory per unit of database work (which is
roughly a request in a web application) and pass it a connection from a
connection pool.
Is this a good way to do it? Is it expensive to create them? Or should I be
pooling the factories too?
Also, kinda related... is there a way to close a factory so that updateable
records don't accidently try and use the factories connection when it has
actually been reassigned to somewhere else (from the connection pool). So if
I call MyRecord.store() and it was attached to a closed factory, it will
give an exception.
Thanks, Ryan