I am building an application with Playframework 1.2x
I have to detect if a database operation succeeded or not while doing CRUD operations
Here is what I have till now
public static void create(Args..) {
Model m = new Model(Args..);
m.save();
if(m.id == null) {
// Render failure Response
}else {
// Render Success Response
}
}
public static void read(long id) {
Model m = Model.findById(id);
if(m == null ) {
// Render failure Response
}else{
// Render Success Response
}
}
I am not really sure as to what has to be done for UPDATE and DELETE.
The above method is not very elegant . Is there a better solution for this, like Exceptions ?
I tried shutting down the database server that gives me a PersistenceException is this the way to go ? are there more exceptions similar to this ?
Yes if you have a database connection problem you will have some exception that will be thrown by the ORM. PersistenceException is the root of JPA exceptions so if the exception is detected by the ORM you have this kind of exception.
For me, catching unexpected exceptions is not the way to go. If you have this kind of technical problem, your database will rollback you crud operation and nothing will be save.
Just catch exception that mean something for the user and let the other ones go up, then play will show a generic 500 page in this case