mysql will throw an error when the inserted data is larger than the destination column. This seems, all in all, to be a positive behavior.
However, I would like to cancel this behavior for the duration of one specific transaction – in this tx, I want truncation to happen, if the data is too large.
Is there a way to disable this behavior, but only for one transaction? Here is the documentation of how to this is at the JDBC level:
Starting with Connector/J 3.1.0, the JDBC driver issues warnings or
throws DataTruncation exceptions as is required by the JDBC
specification unless the connection was configured not to do so by
using the property jdbcCompliantTruncation and setting it to false.
The behavior depends on the MySQL session variable
sql_mode. You can change the variable during a connection, and reset it to the original value later. On my system, the default value ofsql_modeis:You can parse this result in your program, remove the item
STRICT_ALL_TABLES, update the value withSET SESSION sql_mode='...', execute the statements and reset the variable at the end of the transaction.Edit by @ripper234:
Here is the code I wrote in spirit of this answer:
It can be wrapped in a prettier form if one is so inclined.