I take it you mean multiple threads will be updating, deleting etc at the same time. In that case you would execute your SQL statements in "Transactions". Here is an example using JSP's. The same principle works for a desktop app.
http://www.java-tips.org/java-ee-tips/javaserver-pages/executing-database-transactions.html
By the way the DB should handle concurrent access itself in a way to keeps the integrity of the data. Transactions should be used if you are performing multiple DML statements which depend on each other. Here is a Sun tutorial
http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html
> I hope you realize that concurrent threads need to
> use different connections, so that, for example, one
> can commit while concurrently another rolls back.
Does the DB protect from this in the way that if one table if being updated it would hold back another update on the same table until the first is finished?
> Does the DB protect from this in the way that if one table if being updated it would hold back another update until the first is finished?
That's called transaction isolation. Different DBMSs achieve this in different ways,
depending on the level of isolation stipulated.
Do you enjoy the occasional dirty read ;-? http://en.wikipedia.org/wiki/Transaction_isolation_level
> > I hope you realize that concurrent threads need to
> > use different connections, so that, for example,
> one
> > can commit while concurrently another rolls back.
>
>
> ...depending on how you've set your isolation levels,
> not?
I meant that if someone has screwed up and was using the same
connection in two threads, you couldn't speak of separate transactions
and the ability to commit just one of them -- there would only be one
transaction at a time on that one thread! I think we can all agree, that's a no-go.
> > > I hope you realize that concurrent threads need
> to
> > > use different connections, so that, for example,
> > one
> > > can commit while concurrently another rolls
> back.
> >
> >
> > ...depending on how you've set your isolation
> levels,
> > not?
>
> I meant that if someone has screwed up and was using
> the same
> connection in two threads, you couldn't speak of
> separate transactions
> and the ability to commit just one of them -- there
> would only be one
> transaction at a time on that one thread! I think we
> can all agree, that's a no-go.
Oh, yes. Icky bad.
> Do you enjoy the occasional dirty read ;-?
> http://en.wikipedia.org/wiki/Transaction_isolation_lev
> el
Yes, but can I get dirty pictures too next time ? ,-)
>I meant that if someone has screwed up and was using the same
>connection in two threads, you couldn't speak of separate transactions
>and the ability to commit just one of them -- there would only be one
>transaction at a time on that one thread! I think we can all agree, that's a no-go.
Something I never considered. Cheers.
Actually, I wasn't thinking of using an SQL database since I haven't studied it. But, would it be a good idea to have one object of type "Database", and use 'synchronize' to control thread access?
Also, the database access needs to be restricted based on different users, so I am using a SecurityManager. I was thinking the structure would look something like this:
security manager
--
database manager
--
database
in this way, a request to retrieve data from the database would need to be requested from my security manager. If the request is allowed, it would be filtered down to the database manager. Is this the best way to do this?
And finally, someone told me that using something like an "event queue" to access data would work but I've never tried this. My database manager thread would process the requests one at a time. would this work well?