Delegate method to execute an INSERT statement returning any auto-generated
primary key values. This method is primarily designed to be called as a delegate
from the
#insertReturningKey(EntityManager,Connection,Class,String,boolean,String,DBParam...)method. The idea behind this method is to allow custom implementations to
override this method to potentially execute other statements (such as getting the
next value in a sequence) rather than the default implementaiton which uses the
JDBC constant, RETURN_GENERATED_KEYS
. Any database which has a
fully-implemented JDBC driver should have no problems with the default
implementation of this method.
Part of the design behind splitting insertReturningKey
and
executeInsertReturningKey
is so that logic for generating the actual
INSERT statement need not be duplicated throughout the code and in custom
implementations providing trivial changes to the default algorithm. This method
should avoid actually generating SQL if at all possible.
This method should iterate through the passed DBParam(s)
to
ensure that no primary key value was explicitly specified. If one was, it
should be used in leiu of one which is auto-generated by the database. Also,
it is this value which should be returned if specified, rather than the value
which would have been generated or null
. As such, this method
should always return exactly the value of the primary key field in the row which
was just inserted, regardless of what that value may be.
In cases where the database mechanism for getting the next primary key value
is not thread safe, this method should be declared synchronized
,
or some thread synchronization technique employed. Unfortunately, it is not
always possible to ensure that no other INSERT could (potentially) "steal" the
expected value out from under the algorithm. Such scenarios are to be avoided
when possible, but the algorithm need not take extremely escoteric concurrency
cases into account. (see the HSQLDB provider for an example of such a
less-than-thorough asynchronous algorithm)
IMPORTANT: The INSERT
Statement must use the specified
connection, rather than a new one retrieved from
#getConnection() or
equivalent. This is because the INSERT may be part of a bulk insertion, a
transaction, or possibly another such operation. It is also important to note
that this method should not close the connection. Doing so could cause the
entity creation algorithm to fail at a higher level up the stack.