Skip to content

Commit

Permalink
util: use @GetGeneratedKeys to retrieve auto-generated history record_id
Browse files Browse the repository at this point in the history
While the implementation is JDBC driver specific, it seems to be working fine
for our use-case (h2, MariaDB and PostgreSQL).

Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
  • Loading branch information
pierre committed Mar 30, 2017
1 parent 6f2721c commit 17c56ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
Expand Up @@ -25,6 +25,7 @@
import org.killbill.billing.util.entity.dao.EntityModelDao;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.BindBean;
import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;

Expand All @@ -33,8 +34,8 @@ public interface HistorySqlDao<M extends EntityModelDao<E>, E extends Entity> {
@SqlQuery
public List<EntityHistoryModelDao<M, E>> getHistoryForTargetRecordId(@Bind("targetRecordId") final long targetRecordId,
@BindBean InternalCallContext context);

@SqlUpdate
public void addHistoryFromTransaction(@EntityHistoryBinder EntityHistoryModelDao<M, E> history,
@GetGeneratedKeys

This comment has been minimized.

Copy link
@sbrossie

sbrossie Apr 4, 2017

Member

I am guessing when this annotation is present JDBI will use the new JDBC 3.0 method getGeneratedKeys() to automatically return the AUTO_INCREMENT key?

This comment has been minimized.

Copy link
@pierre

pierre Apr 5, 2017

Author Member

Yes, although "new" is relative: JSR 54 (JDBC 3.0 specification) was released in 2002! 😸

public Long addHistoryFromTransaction(@EntityHistoryBinder EntityHistoryModelDao<M, E> history,
@BindBean InternalCallContext context);
}
Expand Up @@ -468,13 +468,7 @@ private InternalCallContext retrieveContextFromArguments(final Object[] args) {

private Long insertHistory(final Long entityRecordId, final M entityModelDao, final ChangeType changeType, final InternalCallContext context) {
final EntityHistoryModelDao<M, E> history = new EntityHistoryModelDao<M, E>(entityModelDao, entityRecordId, changeType, clock.getUTCNow());

sqlDao.addHistoryFromTransaction(history, context);

final NonEntitySqlDao transactional = SqlObjectBuilder.attach(handle, NonEntitySqlDao.class);

/* return transactional.getLastHistoryRecordId(entityRecordId, entityModelDao.getHistoryTableName().getTableName()); */
return nonEntityDao.retrieveLastHistoryRecordIdFromTransaction(entityRecordId, entityModelDao.getHistoryTableName(), transactional);
return sqlDao.addHistoryFromTransaction(history, context);
}

private void insertAudits(final TableName tableName, final M entityModelDao, final Long entityRecordId, final Long historyRecordId, final ChangeType changeType, final InternalCallContext contextMaybeWithoutAccountRecordId) {
Expand Down

1 comment on commit 17c56ae

@sbrossie
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.