org.springframework.dao.DataIntegrityViolationException 原因:java.sql.BatchUpdateException:数据截断

org.springframework.dao.DataIntegrityViolationException Caused by: java.sql.BatchUpdateException: Data truncation

我有一个 运行 java 批处理,其中包含以下内容:

应用程序上下文:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > 
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="${example.connection.url}"/>
        <property name="username" value="${example.connection.username}"/>
        <property name="password" value="${example.connection.password}"/>
        <property name="maxActive" value="100"/>
        <property name="maxWait" value="1000"/>         
    </bean>

DAOImpl:

public void batchInsertUsers(final List<UserInfo> userDataList) {
        logger.info("userList size::"+userDataList.size());

        try{
            jdbcTemplate.batchUpdate(insertUserQuery,
                    new BatchPreparedStatementSetter() {
                        public void setValues(PreparedStatement ps, int i) throws SQLException {
                            java.util.Date date= new java.util.Date();
                            UserInfo userData = userDataList.get(i);

                            ps.setString(1, userData.getUid());
                            ps.setString(2, userData.getFirstName());
                            ps.setString(3, userData.getMiddleName());
                            ps.setString(4, userData.getLastName());
                            ps.setTimestamp(5, null);
                            ps.setString(6, "");
                            ps.setTimestamp(7, null);
                            ps.setBoolean(8, false);
                            ps.setString(9, null);
                            ps.setString(10, userData.getUserID());
                            ps.setTimestamp(11,new Timestamp(date.getTime()));
                            userData = null;
                        }

                        public int getBatchSize() {
                            return userDataList.size();
                        }
                    } );
        }catch(Exception e){
            logger.error("Exception occured while inserting in Users_Temp table::",e);
            logger.error(e);
            e.printStackTrace();
        }
    }

insertUserQuery=INSERT INTO USERS_temp(uid, FirstName, MI, LastName, SyncDate, Status, DisabledDateTime, Exclude, Notes, UpdateID, UpdateDateTime) VALUES (?,?,?,?,?,? ,?,?,?,?,?)

它每次都运行良好,但偶尔会因以下异常而停止,我很无能,因为它没有说明任何数据大小超过允许的列大小或违反任何约束。下面是异常跟踪:

    org.springframework.dao.DataIntegrityViolationException:   PreparedStatementCallback; SQL [INSERT INTO USERS_temp(uid, FirstName, MI, LastName, SyncDate, Status, DisabledDateTime, Exclude, Notes, UpdateID, UpdateDateTime) VALUES (?,?,?,?,?,?,?,?,?,?,?)]; Data truncation; nested exception is java.sql.BatchUpdateException: Data truncation
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
        at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:884)
        at com.example.UserDAOImpl.batchInsertUsers(UsersDAOImpl.java:175)
        at com.example.UserServiceImpl.processData(UserServiceImpl.java:184)
        at com.example.UserMain.main(UserMain.java:23)
Caused by: java.sql.BatchUpdateException: Data truncation
        at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
        at org.springframework.jdbc.core.JdbcTemplate.doInPreparedStatement(JdbcTemplate.java:899)
        at org.springframework.jdbc.core.JdbcTemplate.doInPreparedStatement(JdbcTemplate.java:1)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
        ... 5 more

它是一个批处理过程,因此有数千条记录进入数据库。有没有人遇到过类似的情况?如果是,请说明一下。

Table structure

column name       data type      allow nulls   
UID               char(9)        N
FirstName         varchar(50)    N
MI                varchar(1)     Y
LastName          varchar(50)    N
SyncDate          datetime       Y
Status            char(1)        N
DisabledDateTime  datetime       Y
Exclude           bit            N
Notes             varchar(250)   Y
UpdateID          varchar(10)    N
UpdateDateTime    datetime       N

UID 是主键并被索引。

您正在尝试插入(可能)长度超过相应 column 容量的 String。要么记录有关您尝试插入的用户的信息,要么验证要输入的所有数据是否与数据库中列的大小一致。 (如果让我猜的话,我会说中间名最有可能是罪魁祸首,因为它只能容纳 1 个字符。)

顺便说一句,读这个:http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

另外,世界上最长的名字由 600 多个字符组成。