question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PreparedStatement batch exception: missing trigger message

See original GitHub issue

Driver version or jar name

6.2.1.jre7

SQL Server version

Microsoft SQL Server 13.00.4001

Client operating system

Windows 10

Java/JVM version

java version “1.8.0_144” Java™ SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot™ 64-Bit Server VM (build 25.144-b01, mixed mode)

Problem description

The exception thrown when a batch update fails because of a trigger is different based on the Statement type. If it’s a normal Statement, the trigger message is correctly shown. Instead, if it’s a PreparedStatement, the exception doen’t contain the trigger message, so the issue is hard to find. Calling getCause() or getNextException() doesn’t help.

Repro code

https://gist.github.com/NicolaIsotta/f10f9162c9dc89f903adc0df4644e662

The result of running the code is:

java.sql.BatchUpdateException: [Riga Insesistente] - Impossibile inserire, non esiste alcun legame tra la riga inserita e lariga dell'ordine acquisto!!
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeBatch(SQLServerStatement.java:1740)
	at it.emmeduei.Test.testStatement(Test.java:40)
	at it.emmeduei.Test.main(Test.java:67)
com.microsoft.sqlserver.jdbc.SQLServerException: Transazione terminata nel trigger. Il batch è stato interrotto.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1535)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.processResults(SQLServerStatement.java:1210)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.processBatch(SQLServerStatement.java:1201)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatementBatch(SQLServerPreparedStatement.java:2511)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtBatchExecCmd.doExecute(SQLServerPreparedStatement.java:2383)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2478)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:219)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:199)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:2294)
	at it.emmeduei.Test.testPreparedStatement(Test.java:58)
	at it.emmeduei.Test.main(Test.java:68)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
NicolaIsottacommented, Aug 24, 2017

I tested and approved the fix, thanks @v-afrafi . Feel free to use the test case.

1reaction
NicolaIsottacommented, Aug 23, 2017

I think I worked out something near to a reproducer. First, I run this:

create table test_table (
        test_field int
)

Then this:

create trigger test_trigger
on test_table
for insert
as
begin
if (select test_field from inserted) > 10
begin
return
end
RAISERROR ('Custom error message, you should see me. test_field should be higher than 10', 16, 0)
rollback transaction
end

Then I run this class: https://gist.github.com/NicolaIsotta/792c47791511dd918ebc74ebcc576c42 The result is:

Statement exception:
java.sql.BatchUpdateException: Custom error message, you should see me. test_field should be higher than 10
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeBatch(SQLServerStatement.java:1740)
	at it.emmeduei.Test.testStatement(Test.java:39)
	at it.emmeduei.Test.main(Test.java:65)

PreparedStatement exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Transazione terminata nel trigger. Il batch è stato interrotto.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1535)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.processResults(SQLServerStatement.java:1210)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.processBatch(SQLServerStatement.java:1201)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatementBatch(SQLServerPreparedStatement.java:2511)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtBatchExecCmd.doExecute(SQLServerPreparedStatement.java:2383)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2478)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:219)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:199)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:2294)
	at it.emmeduei.Test.testPreparedStatement(Test.java:55)
	at it.emmeduei.Test.main(Test.java:68)

As you can see, the PreparedStatement exception does not contain the custom trigger message.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Batch Inserts Using JDBC Prepared Statements - Vertica
Triggers an exception if used when streaming batch inserts are enabled. Notes. Using the PreparedStatement.setFloat() method can cause rounding errors. If ...
Read more >
ORA-00928: missing SELECT keyword. when inserting record ...
The problem was caused by the number column name since number is a reserved keyword in Oracle, and the exception was sort of...
Read more >
SQL error messages and exceptions - Oracle Help Center
System generated stored prepared statement ' <statement> ' that cannot be dropped using DROP STATEMENT. It is part of a trigger. 42Y83, An...
Read more >
How to fix java.sql.SQLException: Invalid column index ...
here is a sample code example of getting some data from the PreparedStatement SELECT SQL query. here we have put just one placeholder...
Read more >
Spring Batch - Reference Documentation
Message -driven applications are driven by messages retrieved from a message ... In other words, the eleventh skip triggers the exception, not the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found