lundi 4 juillet 2016

Does Statement.RETURN_GENERATED_KEYS generate any extra round trip to fetch the newly created identifier?


JDBC allows us to fetch the value of a primary key that is automatically generated by the database (e.g. IDENTITY, AUTO_INCREMENT) using the following syntax:

PreparedStatement ps= connection.prepareStatement(
    "INSERT INTO post (title) VALUES (?)",
     Statement.RETURN_GENERATED_KEYS
);

while (resultSet.next()) {
    LOGGER.info("Generated identifier: {}", resultSet.getLong(1));
}

I'm interested if the Oracle, SQL Server, postgresQL, or MySQL driver uses a separate round trip to fetch the identifier, or there is a single round trip which executes the insert and fetches the ResultSet automatically.


Aucun commentaire:

Enregistrer un commentaire