zsoltherpai / fluent-jdbc

Java library for efficient native SQL querying (through JDBC)
Other
183 stars 22 forks source link

Error Handling Documentation #77

Open jimmyzzxhlh opened 4 years ago

jimmyzzxhlh commented 4 years ago

Hi,

Is there any documentation in terms of how exceptions should be handled? If I understand it correctly SqlException is wrapped into FluentJdbcSqlException, so to catch an exception it would be something like:

DataSource dataSource = ...
FluentJdbc fluentJdbc = new FluentJdbcBuilder()
    .connectionProvider(dataSource)
    .build();
String queryString = ...
try {
  fluentJdbc.query().update(queryString).run();
} catch (FluentJdbcSqlException e) {
  ...
}

I'm not using the error handler as it is possible that the exception needs to be re-thrown and there is no way to re-throw an exception in a lambda statement.

Thanks for making this library, it makes JDBC SQL queries a lot cleaner!

zsoltherpai commented 4 years ago

Note that some scenarios may result in the more generic FluentJdbcException. Only the ones that are thrown due to SQLException will be FluentJdbcSqlException.

The exceptions should be handled in the way it suits your needs best, the lib is purposely not oppinionated on this. The only scenario when the exceptions - thrown by fluentjdbc or other code - alter the flow is transaction rollbacks.

If handling exception at each query - like in the example you provided - is necessary, do that. Or you can choose to let it propagate higher and deal with there. The checked exceptions are abstracted away for that reason - you can choose where to handle them.

On Sun, May 10, 2020, 1:48 AM Zhongxia Zhou notifications@github.com wrote:

Hi,

Is there any documentation in terms of how exceptions should be handled? If I understand it correctly SQLException is wrapped into FluentJdbcSqlException, so to catch an exception it would be something like:

DataSource dataSource = ... FluentJdbc fluentJdbc = new FluentJdbcBuilder() .connectionProvider(dataSource) .build(); String queryString = ... try { fluentJdbc.query().update(queryString).run(); } catch (FluentJdbcSqlException e) { ... }

I'm not using the error handler as it is possible that the exception needs to be re-thrown and there is no way to re-throw an exception in a lambda statement.

Thanks for making this library, it makes JDBC SQL queries a lot cleaner!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zsoltherpai/fluent-jdbc/issues/77, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPPNRNLSALYX67RW62VZBDRQXTWTANCNFSM4M5AADFQ .

jimmyzzxhlh commented 4 years ago

Thanks for the response! One caveat regarding to abstracting away checked exceptions, is that people may not be aware that they need to handle the exceptions appropriately, which leads me to think that that throwing one single checked SQL exception can be still desirable.