Find if an SQLException was thrown because of a duplicate

前端 未结 9 2481
青春惊慌失措
青春惊慌失措 2021-02-19 11:01

I have a Java program that is agnostic from the database and I need to know, while inserting, if an SQLException was thrown because of a duplicate key.

If I was using a

相关标签:
9条回答
  • 2021-02-19 11:26

    With basic JDBC, there really isn't a way to do what you are saying in a cross-database manner. As you mentioned getErrorCode could be used, but required vendor-specific error codes.

    The only three ways I see to get around this is:

    1. Use some sort of framework that does all of the translating from error code to meaningful exceptions (Hibernate would probably do this, someone else mentioned that Spring does)
    2. Check for the duplicate manually (with a select) prior to doing your insert. (This wouldn't be 100%, as its technically possible that someone could have done an insert after your query).
    3. After you get any sql exception on the insert, try to query for that id. If you can actually find the match - you can be fairly sure that the error you received was due to a duplicate primary key. (Although its possible that there was multiple problems, and that wasn't actually the one that was thrown).

    My recommendation would be to write your code to avoid the problem as much as possible, and then (if absolutely necessary), use #3.

    0 讨论(0)
  • 2021-02-19 11:35

    This is exactly what SQLException.getSQLState() is for. Acoording to Google, "23000" indicates a unique constraint violation in at least MySQL, PostgreSQL, and Oracle.

    0 讨论(0)
  • 2021-02-19 11:36

    I think the ideal solution would be to have the data layer throw a specific exception in this case, perhaps a subclass of SQLException for DuplicateKeyException or something similar.

    If you want to be able to treat different exceptions differently, then you have to throw different exception types (or sub-types) to begin with.

    I think this is an area where the Spring Framework gets things really right: they provide a very rich hierarchy of "database exceptions" all of which extend DataAccessException , with sub-trees of types for "recoverable exceptions", "transient exceptions", "data integrity exceptions", etc etc. This leaves your client code free to catch any (or none) of the exception types which it can handle or care about: exceptions that indicate an error that may not be repeatable if you re-run the transaction, a fatal non-recoverable error, or you can simply catch the root type.

    0 讨论(0)
  • 2021-02-19 11:38

    Am I missing something? If you're using JDBC you should get back a duplicate key exception, regardless of the DB being used.

    Or did you ask how you would determine a dupkey BEFORE you tried teh insert?

    0 讨论(0)
  • 2021-02-19 11:39

    Well, if you can't rely on the exception to tell you why it was thrown, you could test by following the exception with a "select count(*) from table where key = @keyfailedtoinsert;"

    Unfortunately, the exception isn't guaranteed to give you the table name and key name. In some cases, the java code that called called the JDBC driver may never have had them, e.g., if the insert happened wihin a stored procedure, or as in a trigger.

    So you're back to having to trust each JDBC driver's vendor.

    0 讨论(0)
  • 2021-02-19 11:41

    I believe a simple and reliable way is to check if the key exists prior to doing the insert. As you have rightly pointed out, each database has it's own way of reporting the error.

    0 讨论(0)
提交回复
热议问题