Civcraft / Citadel

Do not open issues here; open them on the maintained fork @ DevotedMC
https://github.com/DevotedMC/Citadel
BSD 3-Clause "New" or "Revised" License
6 stars 23 forks source link

Use Connection.prepareStatement(String, Statement.RETURN_GENERATED_KEYS) #133

Closed psygate closed 8 years ago

psygate commented 8 years ago

https://github.com/Civcraft/Citadel/blob/master/src/vg/civcraft/mc/citadel/database/CitadelReinforcementData.java#L548

Handling database key managment in an application is a bad way of doing it (update anomlies, failed inserts and so on, if more than one connection can commit to the table at any given time). The piece of code here relies on the fact that there will only ever be one application and one CitadelReinforcementData object at any given point in time.

Using https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#prepareStatement%28java.lang.String,%20int%29 in conjunction with https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#RETURN_GENERATED_KEYS is a much safer way to handle key generation and managment.

ProgrammerDan commented 8 years ago

Addressed via stored proc. Mysql does not support single-op return generated keys unless inside a stored procedure.