Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Is it possible to run a @TableGenerator in a separate transaction?
Is it possible to run a @TableGenerator in a separate transaction? [message #531363] Tue, 04 May 2010 15:37 Go to next message
Brett Bergquist is currently offline Brett BergquistFriend
Messages: 31
Registered: July 2009
Member
I am running into a deadlock situation where I have multiple transactions going handling different user requests. I am using the
@TableGenerator and the "strategy = GenerationType" to generate ID's. Periodically, I have multiple ID generators for various parts
of the system and periodically I run into a deadlock where one transaction has a lock on one ID generator table row and is waiting
for another one and another transaction has a lock on the other ID generator table row and is waiting for the first. This only
occurs when the generator runs out of pre-allocated ID's and has to update the generator row with the next allocated sequence.

Since I don't really care about ID's not being wasted or holes in the sequence, I was wondering if it is possible to run the ID
allocation in a separate transaction so this deadlock cannot happen?

I supposed I could use just one ID generator table but I think I will still run into issues if I have separate generator rows for
each type of ID that could be assigned.

Any help will be greatly appreciated.

Brett Bergquist
Re: Is it possible to run a @TableGenerator in a separate transaction? [message #531959 is a reply to message #531363] Thu, 06 May 2010 15:22 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I assume you are using TABLE id generation.

You need to enable a sequence connection pool,

"eclipselink.jdbc.sequence-connection-pool"="true"

You can also configure the number of connections, data-source, etc. It must use a non-JTA data source to allow accessing ids outside of the transaction context.

see,
http://www.eclipse.org/eclipselink/api/2.0.1/org/eclipse/per sistence/config/PersistenceUnitProperties.html#JDBC_SEQUENCE _CONNECTION_POOL

Also if you use SEQUENCE id generation it will not have this issue.


James : Wiki : Book : Blog : Twitter
Re: Is it possible to run a @TableGenerator in a separate transaction? [message #532029 is a reply to message #531959] Thu, 06 May 2010 19:59 Go to previous messageGo to next message
Brett Bergquist is currently offline Brett BergquistFriend
Messages: 31
Registered: July 2009
Member
Thanks James. I am using the TABLE ID generation. I was previously using IDENTITY ID generation and my database is Derby. But
Derby has an issue with concurrent inserts with IDENTITY columns:

https://issues.apache.org/jira/browse/DERBY-4437

Derby does not yet support SEQUENCE ID generation.

So I decided to switch over to TABLE ID generation and ran into this problem. I researched and found out about the sequence pool,
but it appears that this is available after 1.1.3. I cannot move to 1.2 or 2.0.2 because my environment is using Glassfish v2.1.x
So I was stuck.

I took a look at the source changes between 2.0.2 and 1.1.3 for this support and I built a hard coded session customizer that
enables the sequence connection pool and using a non-JTA data source. I think I got that right in that in Glassfish, I created a
new database connection pool and set the "Non Transactional Connections" capability. So my hard coded session customizer looks like:

public class CanogaEclipseLinkSessionCustomizer implements SessionCustomizer {

public void customize(Session session) throws Exception {
if (session.isServerSession()) {
ServerSession serverSession = (ServerSession) session;
serverSession.getSequencingControl().setShouldUseSeparateCon nection(true);
String sequenceDataSource = "jdbc/csemdbNonJTA";
DatasourceLogin login = (DatasourceLogin) serverSession.getLogin().clone();
login.dontUseExternalTransactionController();
JNDIConnector jndiConnector = new JNDIConnector(sequenceDataSource);
login.setConnector(jndiConnector);
serverSession.getSequencingControl().setLogin(login);
}
}
}

and this appears to be working with my testing even with Eclipslink 1.1.3. Is this all that I need to do besides number of
connections, etc.

Thanks in advance for the help.

Brett

James wrote:
> I assume you are using TABLE id generation.
>
> You need to enable a sequence connection pool,
>
> "eclipselink.jdbc.sequence-connection-pool"="true"
>
> You can also configure the number of connections, data-source, etc. It
> must use a non-JTA data source to allow accessing ids outside of the
> transaction context.
>
> see,
> http://www.eclipse.org/eclipselink/api/2.0.1/org/eclipse/per sistence/config/PersistenceUnitProperties.html#JDBC_SEQUENCE _CONNECTION_POOL
>
>
> Also if you use SEQUENCE id generation it will not have this issue.
>
Re: Is it possible to run a @TableGenerator in a separate transaction? [message #532575 is a reply to message #531363] Mon, 10 May 2010 13:37 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

That looks ok.


James : Wiki : Book : Blog : Twitter
Re: Is it possible to run a @TableGenerator in a separate transaction? [message #532611 is a reply to message #532575] Mon, 10 May 2010 14:34 Go to previous message
Brett Bergquist is currently offline Brett BergquistFriend
Messages: 31
Registered: July 2009
Member
Thanks for your time!

James wrote:
> That looks ok.
>
Previous Topic:CreateNativeQuery ResultSet mapping
Next Topic:JPA build entity manager with a specific JDBC connection instance
Goto Forum:
  


Current Time: Mon Dec 22 16:27:11 GMT 2014

Powered by FUDForum. Page generated in 0.02049 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software