Skip to content
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

getGeneratedKeys does not work with sequence #656

Open
sergeykad opened this issue Mar 13, 2018 · 15 comments
Open

getGeneratedKeys does not work with sequence #656

sergeykad opened this issue Mar 13, 2018 · 15 comments
Assignees
Labels
Enhancement An enhancement to the driver. Lower priority than bugs. External Issue is due to an external source we do not control.

Comments

@sergeykad
Copy link

Driver version or jar name

6.4.0.jre8

SQL Server version

2012 Express

Table schema

ALTER TABLE [dbo].[results] ADD CONSTRAINT [DF_results_result_id] DEFAULT (NEXT VALUE FOR [results_result_id_seq]) FOR [result_id]
GO

Problem description

PreparedStatement.getGeneratedKeys() does not work with PK which uses sequence.
The generated key is always returned as zero.

@peterbae
Copy link
Contributor

Hi @sergeykad, thanks for reporting this issue. I created a sequence, and a table with a primary key and tried to reproduce the issue, the the getGeneratedKeys method seems to work for me. Could you provide the dbo.results table schema, the sequence, and your repro code for this problem?

@sergeykad
Copy link
Author

Hi @peterbae, thanks for the quick reply. It may take me some time to create an example, but according to the documentation, only IDENTITY type is supported.
https://docs.microsoft.com/en-us/sql/connect/jdbc/using-auto-generated-keys

@peterbae
Copy link
Contributor

Hi @sergeykad, were you able to find the time to create a small repro case for this problem? Thanks in advance.

@sergeykad
Copy link
Author

sergeykad commented Mar 21, 2018

@peterbae Sorry, but I probably will be unable to do it before April.
Can you comment on the link that I provided earlier?

@peterbae
Copy link
Contributor

Hi @sergeykad. In SQL Server, I believe you have to use the IDENTITY keyword to have a primary key that increments itself (which is also the case I tested myself). The page you linked explains that getGeneratedKeys only works against a table with an IDENTITY key, so that the method knows which column to keep track of (which is correct). Perhaps I'm missing something here - could you show me the table definition + the sequence that you created in SQL Server to increment your primary key column (without the IDENTITY keyword, I presume)? Thanks in advance.

@peterbae peterbae self-assigned this Mar 27, 2018
@sergeykad
Copy link
Author

sergeykad commented Mar 28, 2018

Hi @peterbae , you can use the following SQL to create the table:

CREATE SEQUENCE [results_result_id_seq] START WITH 1;

CREATE TABLE [results](
[result_id] [bigint]  PRIMARY KEY,
[data] [nvarchar](max) NOT NULL);

ALTER TABLE [results] ADD  CONSTRAINT [DF_results_result_id]  DEFAULT (NEXT VALUE FOR 
 [results_result_id_seq]) FOR [result_id];

Run the following SQL to check that PK is generated correctly INSERT INTO results (data) VALUES ('my data');

@peterbae
Copy link
Contributor

peterbae commented Mar 28, 2018

Hi @sergeykad, thanks a lot for your quick reply. I looked through the JDBC specs and you're correct in that our getGeneratedKeys method should return a ResultSet with a column for each generated value (including all the automatically generated values and not just from the IDENTITY row).

I actually recall looking into a similar issue reported before, and it turns out we already have an issue open that addresses a similar case (#245). We will keep the interaction with sequence in mind when we fix that problem, and will keep this issue open until all the related issues have been solved. Thanks for you help!

@mattiamoretta
Copy link

Hi all, having the same issue.. Do we roughly now when this fix will be tackled?

Thanks a lot!

Mattia

@ulvii
Copy link
Contributor

ulvii commented Aug 9, 2019

Hi @mattiamoretta ,
Based on our initial investigations, the issues are happening because of the limitations on the server-side. We are looking into whether anything can be done in the driver as a workaround. We will update the thread with our findings, please stay tuned.

@rene-ye
Copy link
Member

rene-ye commented Sep 9, 2019

Hi @mattiamoretta, this issue isn't being worked on by the team. It requires a server side change regarding how generated keys are returned, and there's nothing we can do in the driver as of now. The team has explored workaround options such as adding OUTPUT clauses to user sql, but these workarounds were deemed unreliable and bad bandage fixes. As of now, the team will attempt to start a discussion with the SQL Server team to see what we can do from there.

@hfazai
Copy link

hfazai commented Jan 19, 2020

I have the same issue. getGeneratedKeys() returns null for column that has been set with NEXT VALUE of a sequence.
Does anyone found a solution or workarround for that ?

Tapac added a commit to JetBrains/Exposed that referenced this issue Jan 25, 2020
SQLServer jdbc driver doesn't support sequences in return values (microsoft/mssql-jdbc#656)
Tests fixed
@gabrielgt3k
Copy link

Have same issue here... is there any forecast to fix?

@praskutti
Copy link

As a workaround, why dont you get the NEXT VALUE FOR Sequence as a first step and use that in the INSERT statement ? This might create some RACE conditions I believe, but if you control the INSERTs, then you can use this a temporary workaround.

@sergeykad
Copy link
Author

@praskutti This indeed creates a race condition and you usually want to avoid these. My solution was to execute SELECT after INSERT, but it is not optimal from the performance perspective.
I am also skeptical about how temporal this solution will be. It will be soon 3 years since the issue was opened.

@lilgreenbird lilgreenbird added External Issue is due to an external source we do not control. Enhancement An enhancement to the driver. Lower priority than bugs. labels Jun 15, 2022
@sproket
Copy link

sproket commented Jun 24, 2022

How is this issue still open after 4 years!? Can you guys just follow the spec already!

@github-project-automation github-project-automation bot moved this to To be triaged in MSSQL JDBC Aug 28, 2024
@Jeffery-Wasty Jeffery-Wasty moved this from To be triaged to Backlog in MSSQL JDBC Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement An enhancement to the driver. Lower priority than bugs. External Issue is due to an external source we do not control.
Projects
Status: Backlog
Development

No branches or pull requests

10 participants