users@glassfish.java.net

Re: JPA concurrent INSERT

From: <forums_at_java.net>
Date: Thu, 11 Nov 2010 08:45:20 -0800

I think I've fixed it.   I have a GenericJpaDaoImpl class and I added a
lockTable() method to it:

 

public void lockTable() { try { String tableName =
entityTypeClass.getAnnotation(Table.class).name(); Query query =
entityManager.createNativeQuery(                     "LOCK
TABLE " + tableName + " IN ACCESS EXCLUSIVE MODE;");            
query.executeUpdate();         } catch (Exception ex) {
            ex.printStackTrace();         }     }
 

The syntax might be specific to PostgreSQL, not sure.    I call that
method early in my transaction for the StatsDao and it gets an exclusive
table lock until the transaction is comitted or rolled back.    I don't
think the performance impact will be negligible in our system.

The only issue I have is the first time I try to use the system after
re-deploying I get:

org.postgresql.xa.PGXAException: Error preparing transaction] on Resource
[prepare] operation.

All subsequent tries work without an exception.    I'm using XA datasource
because my service queries two databases.  I don't care about transaction
coordination since it's mostly reads, but JPA or JDBC complains when I'm not
using XA datasources for this.

 

Thanks,

Ryan


--
[Message sent by forum member 'rdelaplante']
View Post: http://forums.java.net/node/715588