我有一个正在运行的java批处理过程,其中包含以下内容:
的ApplicationContext:
<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$4.doInPreparedStatement(JdbcTemplate.java:899)
at org.springframework.jdbc.core.JdbcTemplate$4.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是主键并已编入索引。
答案 0 :(得分:0)
您正在尝试插入(可能)String
,其长度超过相应的column
容量。记录有关您尝试插入的用户的信息,或者验证要输入的所有数据是否与数据库中列的大小相对应。 (如果我猜,我会说中间名是最可能的罪魁祸首,因为它只有1个字符的空间。)
顺便一提,请阅读:http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
另外,世界上最长的名字由600多个字符组成。