How can I get in the SQL statement that raised the SQLException using the Postgres JDBC driver in Java?

Background

In my current project, a server product without a GUI, I am trying to write in better error handling. Errors are currently logged and usually not read by users.

We use PostgreSQL as our database backend, and we access it using direct JDBC and DAO calls through the database pool. Most database-related exceptions are wrapped in a common DatabaseExceptionclass that implements RuntimeExceptionand tries to pull debugging information and status information from the exception that was passed. In our particular case, he will get access to the basic PostgreSQL database driver - PSQLException. So far, this approach has worked well to get more details on what caused the database error, with the notable exception described below.

In addition, since we have very specific performance requirements and legacy support, we have many custom SQL magic , which makes subsequent stack tracing more intense, but not impossible or complicated.

Problem described

I noticed that when we get SQLExceptionthe result of an erroneous SQL statement, the driver implementation does not return the SQL statement that caused the error. After a short search, I found out that there is a way to drop the PostgreSQL driver into debug mode at startup and show the properties of its internal query. However, we don’t want to run the driver in debug mode in our production environment (and, frankly, I couldn’t figure out how to run it in freakin mode!).

Question

- ? , - , ? , ? , , , , , , .

+3
6

, , , Exception, . , .

, , . , Exception, DatabaseException, triggeringSQLStatement getter setter, , , PostgreSQL, DatabaseException, , triggeringSQLStatement , initCause() DatabaseException, , PostgreSQL, ; DatabaseException, , , , , , SQL, . Java Exception. , , , Java Exception.

, SQL-, , , , - , , . , .

:. SQL , , , DatabaseException toString() ( , , , ), SQL, , , .

+1

, SQL- Custome, SQLException. , - , SQL.

+3

, , p6spy. jdbc . , JDBC, JDBC. , .

http://www.p6spy.com/

+1

DAO, , ?

. SQL- , ( TRACE DEBUG) . DAO ().

0

JDBC (log4j)?

, SQL-, SQL . .

. SOAP, RMI, Corba .. , .. !

, , , SocketAppender . , snooper.

0

, , pgFouine, , Postgres

0

Source: https://habr.com/ru/post/1706790/


All Articles