-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JDBC]Data inconsistency caused by JDBC calling stored procedures #1899
Comments
fixed microsoft#1899 When executing the stored procedure, force the return of true in the dodone method of the nextresult class to allow the message parsing to continue.
Hi @winchannelBj, Thank you for the issue, information and the linked pull request. We'll take a look at all of this and get back to you with a decision. My initial thoughts are that the change proposed may resolve the issue, but might not be the best solution, and so we'll also look into other ways to resolve the problem. |
Thanks for your reply. I found that my change can not work for the bellow stored procedure. CREATE PROC [dbo].[PROC_MS_JDBC_TEST_2] When the JDBC code executes the stored procedure, the tdsreader. Peektokentype() method returns TDS.TDS_ COLMETADATA,which causes the while loop to be terminated. Therefore, the exception information of the stored procedure could not be captured. class:
|
Thank you for the information. How much of a problem is this issue to your current workflow? We've currently backlogged this for at least the next few weeks, however I saw that there was an additional IcM incident raised citing this issue. Please let us know the effect of this issue so we can plan accordingly. |
Hi @winchannelBj, When dealing with multiple resultSets in a store procedure, as you have above, the approach is a bit different.
With this the error is shown correctly. For more information, please see: Using Multiple Result Sets. We'll close this issue and associated PR, but feel free to comment if there are additional questions. |
Thank you for your attention to this issue.If you need any help, you can leave me a message. This problem has a great impact on our system. We use a large number of stored procedures, and many systems have been deployed. The example you provided still doesn't work. Is there anything wrong ?
|
What exactly is the error you're trying to get? Is it not "cannot insert duplicate key"? Without any changes, I was able to get the same results you were, the test passing when it shouldn't. With the changes I proposed, I get the following: Is this not the desired result? EDIT: I've confirmed this has the same result with the latest code snippet you provided. |
This question comes up frequently and there are doc and wiki pages for it: Some past issue references: #367, #399, #826, #937, #995, #1171 |
Thanks!It works. Change the code like this: try (Connection connection = DriverManager.getConnection(URL); Statement statement = connection.createStatement()) { |
Right, that looks like it would process all the results as well. Is any additional assistance needed on this issue? If not, we'll move forward with closing it. |
[JDBC]Data inconsistency caused by JDBC calling stored procedures
Driver version
All versions.
SQL Server version
All versions.
Problem description
I have a table and a stored procedure as bellow:
CREATE TABLE [dbo].[TEST_USER](
[ID] [bigint] NOT NULL,
[USER_NAME] nvarchar NULL,
[USER_PWD] nvarchar NULL,
CONSTRAINT [PK_TEST_ACTIVITY] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- Create unique key for the user_name field.
CREATE UNIQUE NONCLUSTERED INDEX [IX_TEST_ACTIVITY] ON [dbo].[TEST_USER]
(
[USER_NAME] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[PROC_MS_JDBC_TEST]
AS
BEGIN
SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO TEST_USER(ID,USER_NAME,[USER_PWD])
VALUES(1,'unique_error_name','1');
-- The second insert statement will raise an exception.
INSERT INTO TEST_USER(ID,USER_NAME,[USER_PWD])
VALUES(2,'unique_error_name','2');
COMMIT
END
My jdbc code like this:
public class SQLServerJdbcTest {
}
Expected behavior
In my java code,an exception that from the database will be catched.
Actual behavior
Executing the java code above,i can not get any exception. In other words, the Java code is executed successfully, but the database actually has an exception. The behavior of JDBC is somewhat strange and may cause inconsistent data of the system.
Any other details that can be helpful
Through debugging the trace code, I found that the message returned by the stored procedure to the client contains multiple results. Whether the JDBC client throws an error depends on the message parsing. The parsing is handled through a while loop. When the JDBC code does not detect the database exception in the condition of exiting the loop for the first time, the client code will not catch the exception.
In the stored procedure, you can avoid returning multiple results to the JDBC client by adding a “SET NOCOUNT ON” statement.If we do not encounter similar problems, no one knows that we must deal with them in this way.
The text was updated successfully, but these errors were encountered: