Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Database help  (Read 1942 times)
0 Members and 1 Guest are viewing this topic.
Offline jah

Junior Newbie

Java games rock!

« Posted 2003-02-21 10:58:57 »

I hope this is the right place for this...

I want to implement java program to use a mysql DB, and need to make sure of a few things first.

Is it possible to lock a record when it is being written to, and if another process attempts to write to the same record, they can be notified that it is locked, so they can just read the record instead?

Ive read alot of the JDBC tutorials, and cant find if this is possible. I know you can lock a record, and a process will wait until it becomes 'unlocked' but I need to be able to find out if it is locked or not.

Thanks for any help
Offline GergisKhan

Junior Devvie

"C8 H10 N4 O2"

« Reply #1 - Posted 2003-02-21 12:00:38 »

Here are a couple of important lines from the documentation:

Sets this connection's auto-commit mode. If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback. By default, new connections are in auto-commit mode. The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet, the statement completes when the last row of the ResultSet has been retrieved or the ResultSet has been closed. In advanced cases, a single statement may return multiple results as well as output parameter values. In these cases the commit occurs when all results and output parameter values have been retrieved.

What this means is, if you don't setAutoCommit( false ) before you start, EVERY SINGLE TRANSACTION commits to the database immediately every time you write to it, and every read results in a read from the database, as if you were using the command line interpreter for MySQL.

public static final int TRANSACTION_READ_UNCOMMITTEDDirty reads, non-repeatable reads and phantom reads can occur. This level allows a row changed by one transaction to be read by another transaction before any changes in that row have been committed (a "dirty read"). If any of the changes are rolled back, the second transaction will have retrieved an invalid row.

This is a flag you can set using setTransactionIsolation.  Theoretically you can have "dirty reads" meaning that before a transaction commits you can be reading the data in other transactions.  This is somewhat dangerous in my mind.

To get to what you might be trying to do, are you asking to fail a transaction if someone else is writing to it?  Are you trying to prevent a "block" on a transaction?

If so, I think you need to control transactions yourself, and look for a transaction that throws an exception (the SQLSTATE is being elusive right now, so I don't know which error to look for), and then in your catch clause, do the read information you want to do.


"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Offline jah

Junior Newbie

Java games rock!

« Reply #2 - Posted 2003-02-24 08:13:55 »

Thanks for your help!

I am trying to fail atransaction if someone else is writing to it. So that I can then allow them to read the data instead.

So I should just catch the error from this, and setup a read data instead?

Thanks again.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GergisKhan

Junior Devvie

"C8 H10 N4 O2"

« Reply #3 - Posted 2003-02-25 18:36:12 »

Yah, I think that's the procedure you'd have to follow.  Doesn't seem like you can convert midstream; the first one (the write) has to fail.


"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (15 views)
2015-07-29 04:35:27

Riven (37 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (29 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!