>> JDBC transaction auto-locks the tables for you to prevent concurrent modification. Is this trueNo, 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.