SQL Exception not being caught [message #1172392] |
Tue, 05 November 2013 17:26  |
Eclipse User |
|
|
|
I have a method that inserts in a join table identified in a ManyToMany relationship as follows:
try {
User user = userService.get(id);
user.getGroups().add(newGroup);
} catch (SQLException e) {
log.log(Level.SEVERE, e.getMessage());
} catch (Exception e) {
log.log(Level.SEVERE, e.getMessage());
}
On the database, there is a join table with columns id_user and id_group as primary key.
The code works and when inserting a duplicated record it fails, as expected, with message:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "user_groups"
Detail: Key (id_user, id_group)=(3, 2) already exists.
But unfortunately, this exception is not being caught on the try/catch, instead it sends 500 and the stack trace to the browser.
What might be happening?
[Updated on: Tue, 05 November 2013 17:29] by Moderator
|
|
|
|
|
|
Re: SQL Exception not being caught [message #1175675 is a reply to message #1173834] |
Thu, 07 November 2013 17:54  |
Eclipse User |
|
|
|
Hi Chris, that makes sense. I am using Java EE 7, and the code above is being called in a JAX-RS rest request (so, request scooped). I don't have fine control of transactions because I use the following code to get the EntityManager instance:
@Produces
@PersistenceContext(type= PersistenceContextType.TRANSACTION)
private EntityManager em;
It is then injected into my service classes. This is how it's done in most tutorials. Would you recommend a different approach, in order to have control of transactions, or is it ok to flush() after entity operations, right before hitting return?
|
|
|
Powered by
FUDForum. Page generated in 0.13152 seconds