Іван Гладуш
Іван Гладуш

Reputation: 385

The difference between Connection.getWarnings() and Statement.getWarnings() in JDBC?

I don't understand what the difference between in Connection.getWarning() and Statement.getWarnings() ? Is the main difference that Connection.getWarning contains warnings for all statements and Statement .getWarnings contains warnings for single statement?

Upvotes: 0

Views: 605

Answers (2)

Mark Rotteveel
Mark Rotteveel

Reputation: 109021

Connection.getWarnings returns connection-level warnings, while Statement.getWarnings returns statement-level warnings. There is also ResultSet.getWarnings, which (surprise) returns result-set-level warnings. In other words, if a method called on a Connection produces a warning, it is added on the connection, for a method called on a Statement on the statement, and for a method of a ResultSet on the result set.

In general, warnings produced by a Statement should not be reported on the Connection, and warnings on a ResultSet should not be reported on the Statement. See for example Statement.getWarnings() which says:

Note: If you are processing a ResultSet object, any warnings associated with reads on that ResultSet object will be chained on it rather than on the Statement object that produced it.

and ResultSet.getWarnings() which says:

Note: This warning chain only covers warnings caused by ResultSet methods. Any warning caused by Statement methods (such as reading OUT parameters) will be chained on the Statement object.

The JDBC specification is not very detailed on warnings, it only says (JDBC 4.3, section 8.2 SQLWarning):

When a method generates an SQLWarning object, the caller is not informed that a data access warning has occurred. The method getWarnings must be called on the appropriate object to retrieve the SQLWarning object. However, the DataTruncation sub-class of SQLWarning may be thrown in some circumstances, see Section 8.3 “DataTruncation” on page 8-46 for more details.

If multiple data access warnings occur, they are chained to the first one and can be retrieved by recursively calling the SQLWarning.getNextWarning method. If there are no more warnings in the chain, getNextWarning returns null.

Subsequent SQLWarning objects continue to be added to the chain until the next statement is executed or, in the case of a ResultSet object, when the cursor is repositioned, at which point all SQLWarning objects in the chain are removed.

and in section 8.3 DataTruncation:

When data truncation occurs on a read from the data source, a SQLWarning is reported.

and in section 15.1.1 ResultSet Types:

If the driver does not support the type supplied to the methods createStatement, prepareStatement, or prepareCall, it generates an SQLWarning on the Connection object that is creating the statement.

Similar text can be found in 15.1.2 ResultSet Concurrency and 15.1.3.1 Determining ResultSet Holdability.

Sometimes the JDBC API documentation explicitly mention warnings (eg Connection.setClientInfo​(String, String) and ResultSet.next()).

Although not specified by JDBC, drivers will commonly also report warnings during connect on the Connection.

Upvotes: 2

Azad
Azad

Reputation: 171

The Object type returned by the getWarnings method of Connection, Statement is not different. It is of type SQLWarning.

But depending on the context it has been called it differs.

  1. Connection.getWarnings will retrieve database connection related warnings like database connection string doesn't have useSSL flag (warning logged in mysql).
  2. Statement.getWarnings will retrieve sql statement related warnings like while executing statements inserting or deleting.

Upvotes: 1

Related Questions