Java conccurency with database stored objects

Antoine

It's probably a the duplicate of a subject but i can't find an answer in google. (Probably using the wrong words)

I'm working on a Java REST API and i have two java object stored in my database. An appointment and an availability. To make an appointment i need to check if my user is available.

public void addAppointment(Date startDate, Date endDate, User user)
{
    if(availabilityService.isAvailable(startDate, endDate, user)
        dao.addAppointment(startDate, endDate, user);
}

Everything is ok if I only have one instance of my services. But what if i had two instance and they check at the same time my method before storing the new appointment ?

How to manage the concurrency with object that are stored in my database ?

I have no idea if it will change something but i am planning to use Google App Engine or AWS to deploy my application.

Thx.

Arthur Cinader

The typical way to handle this issue is by versioning the records. A strategy called optimistic locking. When you read the user, there should a be a version for that user. You then make the changes you want. When you go to write it, if the version numbers don't match, then you know that someone else has updated that record since you read it. The write fails. You re-read the record to get the updates and you try again (if it makes sense with the new data).

Many libs for dealing with databases will have this built in:

For AWS Dynamo DB: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html

If you plan to bring your own db, hibernate is a java ORM that implements this type of locking: http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#entity-pojo-optlock

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Where to instantiate objects stored in database?

From Dev

Execute Java code that is stored in the database

From Dev

Java EE containers don't allow using Java SE conccurency API

From Dev

Java Hashmap two identical objects stored seperate

From Dev

How do Java Objects get stored in Fields?

From Dev

Can't Access Objects Stored In Real Time Database

From Dev

How to store Java Objects in JavaDB Derby Database?

From Dev

Creating Java objects from Database SQL query

From Dev

Creating Java objects from Database SQL query

From Dev

how to display date chosen from java JXDatePicker and how to stored into database?

From Dev

How to only get the day from the date stored in database in java?

From Dev

Multiple threads checking map size and conccurency

From Dev

Deleting a stored procedure in the database

From Dev

User rights stored in Database

From Dev

No information stored in SQLite Database

From Dev

Interpolating a string stored in a database

From Dev

Scraping an element as it is stored in the database

From Dev

Adding a stored procedure to a database

From Dev

display image stored in database

From Dev

Stored procedure issue in database

From Dev

character not stored as NULL in database

From Dev

Stored Procedure not updating database

From Dev

Parsing stored procedures in a database

From Dev

User is not stored in the database Spring

From Dev

Data not stored into realm database

From Dev

How is data stored in a database?

From Dev

How to put Java objects back into SQL database using ORMLite?

From Dev

SQL Database vs Writing Java Serialized Objects To File?

From Dev

(Java) How do I make a database delete a stored value until the value has been stored more than 5 times?

Related Related

  1. 1

    Where to instantiate objects stored in database?

  2. 2

    Execute Java code that is stored in the database

  3. 3

    Java EE containers don't allow using Java SE conccurency API

  4. 4

    Java Hashmap two identical objects stored seperate

  5. 5

    How do Java Objects get stored in Fields?

  6. 6

    Can't Access Objects Stored In Real Time Database

  7. 7

    How to store Java Objects in JavaDB Derby Database?

  8. 8

    Creating Java objects from Database SQL query

  9. 9

    Creating Java objects from Database SQL query

  10. 10

    how to display date chosen from java JXDatePicker and how to stored into database?

  11. 11

    How to only get the day from the date stored in database in java?

  12. 12

    Multiple threads checking map size and conccurency

  13. 13

    Deleting a stored procedure in the database

  14. 14

    User rights stored in Database

  15. 15

    No information stored in SQLite Database

  16. 16

    Interpolating a string stored in a database

  17. 17

    Scraping an element as it is stored in the database

  18. 18

    Adding a stored procedure to a database

  19. 19

    display image stored in database

  20. 20

    Stored procedure issue in database

  21. 21

    character not stored as NULL in database

  22. 22

    Stored Procedure not updating database

  23. 23

    Parsing stored procedures in a database

  24. 24

    User is not stored in the database Spring

  25. 25

    Data not stored into realm database

  26. 26

    How is data stored in a database?

  27. 27

    How to put Java objects back into SQL database using ORMLite?

  28. 28

    SQL Database vs Writing Java Serialized Objects To File?

  29. 29

    (Java) How do I make a database delete a stored value until the value has been stored more than 5 times?

HotTag

Archive