Search code examples
springhibernatedb2spring-batchdata-persistence

Hibernate with Spring batch - select scope_identity() appended after Insert query


I have a VO with the following fields -

HD_REC_ID
REC_TYP_CD
SRC_SYS_NM
SRC_TYP_CD
RCV_SYS_CD
JOB_STTS_CD
IDX_FILE_NM
IDX_FILE_LOC_TXT
XTRCT_DT
XTRCT_TM
DTL_REC_CNT
ATCH_CNT
ERR_MSG_TXT
PROC_MODE_CD
INVLD_HD_REC_TXT
CRT_TS

The annotations used are -

@Id
@Column(name = EntityConstants.HD_REC_ID)
@GeneratedValue(strategy = GenerationType.AUTO)
private int headerId;

@Generated(value = GenerationTime.INSERT)
@Column(name = EntityConstants.CRT_TS)
@Temporal(TemporalType.TIMESTAMP)
private Date creationTime;

Of these the first and last fields(HD_REC_ID and CRT_TS) are auto-generated. While inserting the value to DB, this is what i get on the console (edited console).

[8/5/13 15:22:12:666 IST] 00000063 SystemOut     O Hibernate: insert into T_HD_REC (ATCH_CNT, ERR_MSG_TXT, XTRCT_DT, XTRCT_TM, IDX_FILE_LOC_TXT, IDX_FILE_NM, INVLD_HD_REC_TXT, JOB_STTS_CD, PROC_MODE_CD, DTL_REC_CNT, REC_TYP_CD, RCV_SYS_CD, SRC_SYS_NM, SRC_TYP_CD) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) select scope_identity()
[8/5/13 15:22:13:046 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:100) 2013-08-05 15:22:13,045 -- WARN -- SQL Error: -199, SQLState: 42601
[8/5/13 15:22:13:047 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:101) 2013-08-05 15:22:13,046 -- ERROR -- DB2 SQL error: SQLCODE: -199, SQLSTATE: 42601, SQLERRMC: SELECT;;FOR <END-OF-STATEMENT> NOT ATOMIC
[8/5/13 15:22:13:047 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:100) 2013-08-05 15:22:13,047 -- WARN -- SQL Error: -516, SQLState: 26501
[8/5/13 15:22:13:048 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:101) 2013-08-05 15:22:13,047 -- ERROR -- DB2 SQL error: SQLCODE: -516, SQLSTATE: 26501, SQLERRMC: null
[8/5/13 15:22:13:048 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:100) 2013-08-05 15:22:13,048 -- WARN -- SQL Error: -518, SQLState: 07003
[8/5/13 15:22:13:049 IST] 00000063 SystemOut     O (JDBCExceptionReporter.java:101) 2013-08-05 15:22:13,048 -- ERROR -- DB2 SQL error: SQLCODE: -518, SQLSTATE: 07003, SQLERRMC: null


com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -199, SQLSTATE: 42601, SQLERRMC: SELECT;;FOR <END-OF-STATEMENT> NOT ATOMIC

Read up about SQL error codes here - http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z9.doc.codes%2Fsrc%2Ftpc%2Fn199.htm

-518
THE EXECUTE STATEMENT DOES NOT IDENTIFY A VALID PREPARED STATEMENT
-516
THE DESCRIBE STATEMENT DOES NOT SPECIFY A PREPARED STATEMENT
-199
ILLEGAL USE OF KEYWORD keyword. TOKEN token-list WAS EXPECTED

From what I understand, it is because the stmt select scope_identity() is appended without a preceeding semicolon. But I am not calling this anywhere. This error is happening while I am moving the POC we had created on SQL server to a DBA created database on DB2 9. This worked perfectly on SQL server. There was no select scope_identity() appended after the insert stmt logs while the application was on SQL server.

I am very new to both Hibernate and Spring(Batch). Sorry if this sounds silly to you... but surprisingly, I did not find anything of this type on the net... I checked this link out and got an idea about Identities. What is the difference between Scope_Identity(), Identity(), @@Identity, and Ident_Current?

Any help would be appreciated... Thanks!!!


Solution

  • Did you change dialect from SQLServerDialect to DBDialect? select scope_identity() cames from SQLServerDialect.appendIdentitySelectToInsert()