Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 1666 times)
0 Members and 1 Guest are viewing this topic.
Offline jah

Junior Newbie




Java games rock!


« Posted 2003-02-21 11: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 Member




"C8 H10 N4 O2"


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

Here are a couple of important lines from the documentation:

Quote
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.



Quote
TRANSACTION_READ_UNCOMMITTED
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.

gK

"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 09: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 Member




"C8 H10 N4 O2"


« Reply #3 - Posted 2003-02-25 19: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.

gK

"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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!