How to retrieve the row that caused exception using spring framework's namedParameterJdbcTemplate - spring-boot

I am inserting user information where a few columns have unique constraint on them one of which is email id. In testing when I try to insert the same user twice I get the
org.springframework.dao.DuplicateKeyException
it also does tell me which column violated the unique constraint but it does not give me back the row that triggered the violation. I have seen solution where the user has caught such exception and run a query based on column on which violation was triggered but does spring have anything that will return the row details along with the exception.
I tried looking but there is so much information explaining why the exception happened that I do not find a solution that returns the row that caused the violation.

Related

Log Postgresql sequence value that is missing in main table

Gaps on id column occurs due to the usage of sequences on PostgreSQL as the default value of the table’s primary key. When the transaction rollback the sequence value will not rollback thus the id column will be not sequential. I have tried to eliminate the gaps but it seems to impact the performance. What I am thinking right now is to log those missing IDs along with error message and request payload. As a result, I am struggling getting the ID that was generated by sequence when DB transaction rollback.
Please suggest approach to get the missing sequence ID from DB when function is rollbacked so we can log it.

How to recognize what constraint cause an exception in an insertion in Sqlite

How could I know what type of constraint exception is raised in an insertion in Sqlite. What it caused?. For example if I design a table with an unique constraint and primary key constraint, and I insert a row that violate a constraint, sqlite will raise an SqliteConstraintException, but I would like to know which of both constraints was violated. That would be helpful for creating an error message for the user. Instead of writing functions that validate the row before inserting it. Thank you for your help.
That information is not provided as a field on the Exception. You can use the getMessage() method to get some information about the cause. Like UNIQUE constraint failed: event.id as an example. This is telling you the name and the column which violated the constraint, but not the name of the constraint which is violated.

Codeigniter ORA-00001: unique constraint

This error: Message: oci_execute (): ORA-00001: unique constraint (SCHEMA_NAME.NAME CONSTRAINT) violated
I wonder if I do not how to handle the error more simply, more generic.
Because otherwise I will have to work on each function of the models to check the data before adding, for no duplications and not give the error noted above.
Does anyone know a simple way to solve this?
Thanks.
There is a hint that can be specified that will allow the statement to succeed without inserting the duplicated data. It can be useful for replication or bulk data loading where the job may attempt to insert the same data multiple times. I wouldn't recommend it as part of a user application.
"The IGNORE_ROW_ON_DUPKEY_INDEX hint applies only to single-table INSERT operations. It is not supported for UPDATE, DELETE, MERGE, or multitable insert operations. IGNORE_ROW_ON_DUPKEY_INDEX causes the statement to ignore a unique key violation for a specified set of columns or for a specified index. When a unique key violation is encountered, a row-level rollback occurs and execution resumes with the next input row."

In Postgresql, how do I debug an error that happens after a delete cascades?

I'm deleting this row from a table and it has a bunch of cascades on the FKs and eventually it gives me this error:
ERROR: insert or update on table "foo_route" violates foreign key constraint "foo_route_bar_fk"
SQL state: 23503
Detail: Key (bar_key)=(2176) is not present in table "bar".
foo_route_bar_fk is defined like this:
ALTER TABLE foo_route
ADD CONSTRAINT foo_route_bar_fk FOREIGN KEY (bar_key) REFERENCES bar (bar_key)
MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE;
I'm getting this error by deleting a table unrelated to both of these. I think what's happening is a trigger or a cascade is causing this error, but it's difficult to find out why.
My question is, how do you debug issues like this in postgresql? What are the series of steps that caused this error? Postgresql only tells me the result of the last thing it did before it failed. If this were code the error would give an extremely helpful stack trace. Is there a way to see something like that in Postgresql?
Typically your postgresql log will include the statement which triggered the error. That should allow you to follow the chains of cascading events.
One thing I would suggest however is that you might want to draw out a map of fkeys and look at the mappings of ON events on foreign keys. If it were me I would start with a schema only dump or pg_autodoc output, and go from there. The obvious problem is that you have deletes cascading in cases where it can't so you need to take a look and rethink things here.

org.springframework.dao.DataIntegrityViolationException

while adding some data to table from jsp, only one row of data gets inserted
Also an org.springframework.dao.DataIntegrityViolationException is thrown.
Can you say why this exception is raised?
The exception is in most cases about database constraint violation. - In your case I would guess it is some unique constraint - but this is only guessing.

Resources