You canuse instanceof instead of string comparison.
Then to extract the oracle code, you can use regex to avoid position changes.
Clinton
--
Sent from my mobile device
public boolean insertUser(User user);
public boolean containsUsername(String username);
And with some sql:
SELECT CASE COUNT(Username) WHEN 0 THEN 0 ELSE 1 END
FROM Users
WHERE Username = #{value}
...mybatis type does the rest. Very convenient.
Database isolation level is set to serializable and the contains method is called in the scope of the insert transaction.
try {
UserMapper mapper = session.getMapper(UserMapper.class);
if (mapper.containsUsername(username) {
return false;
} else {
mapper.insertUser(user);
}
session.commit();
return true;
} finally {
session.close();
....I don't know how I came up with the case statement....
Database isolation level is set to serializable.
Well, it's a way to make it, but I think exceptions are that: rare
cases (that must be managed).
This way performs an additional query to check the constraint, but I
prefer to think that most of the times , the input data is right and
I only must manage the exceptional cases. IMHO
What do you think about this ?
Larry gave a good explanation in a different post. In brief we double check "Oops!" exceptions but "AHHs!" crashes our app for good. And its nice to validate a username with ajax before the customer hits the submit button.