调试MysqlDataTruncation异常

时间:2013-01-16 17:06:27

标签: java mysql exception

我使用以下例程在数据库中存储信息:

try{
connection = cpds.getConnection();
pstmt = connection.prepareStatement("select setSomeData(?,?)");
//set data to the statement
res = pstmt.executeQuery();
//process data retrieved
} catch (SQLException ex) {
        System.out.println("SQLException: " + ex);
        ex.printStackTrace();
        return null;
    } finally {
        try {
            pstmt.close();
            connection.close();
        } catch (SQLException ex) {
    }
}

但有时候我会遇到这个例外:

  

SQLException:com.mysql.jdbc.MysqlDataTruncation:数据截断:BIGINT UNSIGNED值在'(tstmp @ 5 - lastEvent @ 11)中超出范围'
  com.mysql.jdbc.MysqlDataTruncation:数据截断:BIGINT UNSIGNED值在'(tstmp @ 5 - lastEvent @ 11)中超出范围'
  在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
  在com.mysql.jdbc.MysqlIO.nextRowFast(MysqlIO.java:2066)
  在com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1922)
  在com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:3414)
  在com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:481)
  在com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3120)
  在com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2288)
  在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2709)
  在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
  在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2134)
  在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2300)
  在com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)

是否可以了解有关异常的更多信息,例如受影响的列和我尝试存储的值?

例如,对于例外:.MySQLTransactionRollbackException: Deadlock found when trying to get lock;

查询SHOW ENGINE INNODB STATUS非常有用......

1 个答案:

答案 0 :(得分:1)

除了SQLSTATE之外,通过JDBC提供的调试信息非常少,以及相应的MySQL错误代码和消息。

https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html


对于第一个例外,最可能的解释是,当尝试将负值存储到UNSIGNED整数数据类型时,它是由MySQL引发的警告(或错误)引起的。

举个例子:

CREATE TABLE foo (val BIGINT UNSIGNED);
INSERT INTO foo (val) VALUES (-4);

0 row(s) affected
Total Time     : 0.030 sec
--------------------------------------------------

1 row(s) affected, 1 warning(s)
Total Time     : 0 sec

Warning Code : 1264
Out of range value for column 'val' at row 1

对于另一个例外,这是有关正在引发的MySQL错误的信息:

Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)
Message: Deadlock found when trying to get lock; try restarting transaction

对于InnoDB表遇到的错误,是的,SHOW ENGINE INNODB STATUS语句的输出可以提供一些见解。但该信息适用于整个数据库,并不一定特定于遇到错误的会话。