Re: [ebean] Does a Transaction lock tables?

1,722 views
Skip to first unread message

Rob Bygrave

unread,
Jun 21, 2012, 6:01:17 PM6/21/12
to eb...@googlegroups.com
>> JDBC transaction auto-locks the tables for you to prevent concurrent modification. Is this true

No, that is not correct.

To create explicit locks (Pessimistic locks) against rows in the DB your SQL query includes "FOR UPDATE" in the SELECT statement. (Google "select for update" etc).  Whether that get translated into row level locks or higher depends on the DB (Postgres, MySql, Oracle etc) and other things.

Now I don't believe you actually need explicit DB locks for your use case - I think you can instead just using "Optimistic Locking" with a @Version property.  However, some rough example of Pessimistic locking is below...



A rough untested code example from memory ... (you need to test this)


Transaction t = Ebean.beginTransaction();
try {
  Query<UserOnline> query = Ebean.createQuery(UserOnline.class);
  query.where() ...

  // to issue a "FOR UPDATE" ... to explicitly lock row(s)
  query.setForUpdate(true);

  // the underlying row in the DB will have an explicit lock on it now
  UserOnline userOnline = query.findUnique();


  // do stuff while you hold the locks


  // commit or rollback to release the locks
  t.commit();

} finally {
  // rollback if some error occured in the try/finally block
  t.end();
}

Cheers, Rob.

On 22 June 2012 08:11, zonedabone <zoned...@gmail.com> wrote:
I'm working on a distributed server system for a game in which a user could potentially log on to two of the servers at once. My plan is to keep a database of who's online and then perform a select/update setting them to online whenever they log in, unless it was already set to online, in which case the user would be disconnected. To accomplish this safely, I am planning on using database locks. It appears from what I'm reading that Ebean Transactions represent JDBC transactions and that the JDBC transaction auto-locks the tables for you to prevent concurrent modification. Is this true, or is there some other method for locking in Ebean? I may just be trying to hard to avoid having to code a session server. Any advice and help is appreciated. Thanks in advance.

zonedabone

unread,
Jun 21, 2012, 7:34:19 PM6/21/12
to eb...@googlegroups.com
Thank you for the quick response.Looking at com.avaje.ebean.Query I can't seem to find setForUpdate(). Am I looking at the wrong Query class, or has it been added since 2.7.3? (I am stuck using this version for now as the libraries I use refuse to update without a good reason)

zonedabone

unread,
Jun 21, 2012, 7:37:04 PM6/21/12
to eb...@googlegroups.com
I guess that I could theoretically just run a manual query for this.

Mike

unread,
Apr 22, 2013, 8:28:59 AM4/22/13
to eb...@googlegroups.com
The answer to this question, even if apparently nobody cares, is that setForUpdate() was added in 2.7.5 and is not even listed in the online javadoc as of today (http://www.avaje.org/static/javadoc/pub/index.html) but this is still the documentation for the 2.6.0 API.

Rob Bygrave

unread,
Apr 22, 2013, 8:51:11 AM4/22/13
to eb...@googlegroups.com
Thanks Mike.

Just as a general comment, I'm pretty sure there were a bunch of questions over the past 6+ months that possibly didn't get answered satisfactorily. If people post on those questions we can have another look at them etc.

Cheers, Rob.

On 23 April 2013 00:28, Mike <miche...@gmail.com> wrote:
The answer to this question, even if apparently nobody cares, is that setForUpdate() was added in 2.7.5 and is not even listed in the online javadoc as of today (http://www.avaje.org/static/javadoc/pub/index.html) but this is still the documentation for the 2.6.0 API.


--
 
---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages