Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C22B11337B69 for ; Tue, 29 Mar 2011 12:21:04 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 12377-05 for ; Tue, 29 Mar 2011 15:20:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id BD3F91337B67 for ; Tue, 29 Mar 2011 12:20:46 -0300 (ADT) Received: by vws1 with SMTP id 1so213150vws.19 for ; Tue, 29 Mar 2011 08:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:from:to:cc:references:in-reply-to :subject:date:mime-version:content-type:content-transfer-encoding :x-priority:x-msmail-priority:x-mailer:x-mimeole; bh=yTrvJWTxlQk5G8Dsfp4nsvH9xqIxHlRw9tEteXmfvL8=; b=JQP8Jb/oSKpVc/DhxrIpncNC81RBeAdI7VTnYHD40O5yGlKdDedSmtadBY2Fb5y3+N icx9LgHuh9QnL+g2IGmBAPCV7+9lSWef+VdUT4xbAperSKPUIfY/OOC1kSGyXvdbFrBJ 4NnKsEY8Paqn+mhnAy047Xhg9yzA/vAKwxjWA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:from:to:cc:references:in-reply-to:subject:date :mime-version:content-type:content-transfer-encoding:x-priority :x-msmail-priority:x-mailer:x-mimeole; b=t/JI/GyIw+sJhFXVji2v/9QS91fwjPpwaVTsPwN45QJtJK3EX3ybr5pWLyfg1j/GRa vI3KAUW6M1cpsxs8PN6RJzWMcr7IVBNZJDVWsc2OTTsjWneAH5oWl/cUo/x2HQLB8NZd o5a5hv+gvRVxrw2XxpbB02Odq9T5lP1mJiM3Q= Received: by 10.52.162.101 with SMTP id xz5mr7504624vdb.151.1301412045008; Tue, 29 Mar 2011 08:20:45 -0700 (PDT) Received: from maumau (p3240-ipbfp303sizuokaden.shizuoka.ocn.ne.jp [124.99.119.240]) by mx.google.com with ESMTPS id v16sm443907vcr.37.2011.03.29.08.20.41 (version=SSLv3 cipher=OTHER); Tue, 29 Mar 2011 08:20:42 -0700 (PDT) Message-ID: <82360DA2A81A4F6197F346F445C71B2C@maumau> From: "MauMau" To: "Dave Cramer" Cc: "Mike Fowler" , References: <0BCE9496109B41DDA29A2E91C3BE4C11@maumau> <4D8E5C16.1060808@mlfowler.com> In-Reply-To: Subject: Re: JDBC gripe list Date: Wed, 30 Mar 2011 00:21:58 +0900 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Windows Mail 6.0.6002.18197 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6002.18263 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.093 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, STOX_REPLY_TYPE=0.439 X-Spam-Level: X-Archive-Number: 201103/108 X-Sequence-Number: 21663 Hello, ----- Original Message ----- From: "Dave Cramer" > So in general I'm having trouble understanding things where the > implementation says we can do whatever we want you have a requirement > ? For example getGeneratedKeys from a batch statement? What is it you > are looking for ? AFAICT we are implementing the spec here. The fact > that mysql does something else is irrelevant. Please note this is just > a talking point since I do believe it is possible for us to return the > generated keys here. I think the most common use case is, or possibly the only real use case is, to get auto-generated primary keys when you insert multiple rows at once into a table which has a sequence. An example is shown in "14.1.4 PreparedStatement Objects" of JDBC 4.0 specification. The following code is a slightly modified version of that example to use auto-generated keys. CODE EXAMPLE 14-2 Creating and executing a batch of prepared statements (slightly modified) -------------------------------------------------- String[] cols = {"emp_id"}; // turn off autocommit con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement( "INSERT INTO employees VALUES (?, ?)", cols); stmt.setInt(1, 2000); stmt.setString(2, "Kelly Kaufmann"); stmt.addBatch(); stmt.setInt(1, 3000); stmt.setString(2, "Bill Barnes"); stmt.addBatch(); // submit the batch for execution int[] updateCounts = stmt.executeBatch(); -------------------------------------------------- Though the table definition is not shown in the spec, assume the one below: CREATE TABLE employees ( emp_id SERIAL PRIMARY KEY, salary INT, name VARCHAR(20) ); Then, pstmt.getGeneratedKeys() is expected to return a ResultSet that contains two rows which consist only of emp_id column. The order of those two rows should correspond to the order of employee records added in the batch with addBatch(). My opinion is that the users should explicitly specify the columns they want to be returned. I don't find it useful to specify Statement.RETURN_GENERATED_KEYS, because the JDBC spec says that JDBC drivers decide which columns are returned (i.e. implementation defined behavior). Please keep this in mind when reading the next. So which columns should be returned when you pass Statement.RETURN_GENERATED_KEYS instead of "cols" to Connection.prepareStatement() in the above example code? What if the table does not have an auto-generated primary key? I think it would be reasonable to return "ctid" system column when the user specifies Statement.RETURN_GENERATED_KEYS, because he/she does not show any intention about what he/she wants. For example, Oracle returns ROWID in this case, as described as follows in the manual: -------------------------------------------------- The getGeneratedKeys() method enables you to retrieve the auto-generated key fields. The auto-generated keys are returned as a ResultSet object. If key columns are not explicitly indicated, then Oracle JDBC drivers cannot identify which columns need to be retrieved. When a column name or column index array is used, Oracle JDBC drivers can identify which columns contain auto-generated keys that you want to retrieve. However, when the Statement.RETURN_GENERATED_KEYS integer flag is used, Oracle JDBC drivers cannot identify these columns. When the integer flag is used to indicate that auto-generated keys are to be returned, the ROWID pseudo column is returned as key. The ROWID can be then fetched from the ResultSet object and can be used to retrieved other columns. -------------------------------------------------- What do you think? Regards MauMau