Search code examples
javamysqldropwizardjdbi

@SqlBatch making multiple inserts in jdbi sql object api


public interface UserDBDao {
    @SqlBatch ("INSERT INTO user (id, name) VALUES (:user.id, :user.name)")
    void createAllUsers(@BindBean("user") List<User> users);
}

When I try to insert multiple rows using the above method, even after using @SqlBatch it is making multiple insert queries. The Sql log is as follows

2019-12-18T09:33:41.375486Z    22 Query SET autocommit=0
2019-12-18T09:33:41.376469Z    22 Query SELECT @@session.transaction_read_only
2019-12-18T09:33:41.376791Z    22 Query INSERT INTO user (id, name) VALUES (1000, 'a0')
2019-12-18T09:33:41.377059Z    22 Query INSERT INTO user (id, name) VALUES (1001, 'a1')
2019-12-18T09:33:41.377248Z    22 Query INSERT INTO user (id, name) VALUES (1002, 'a2')
2019-12-18T09:33:41.377427Z    22 Query INSERT INTO user (id, name) VALUES (1003, 'a3')
2019-12-18T09:33:41.377618Z    22 Query commit
2019-12-18T09:33:41.377903Z    22 Query SET autocommit=1

How to insert all records in single push?


Solution

  • As you can see from the log it opens the transaction and does it in one go. So basically you're achieveing "single push" anyway. Unfortunately you can't union multiple inserts into one unless you want to do it yourself with direct JDBC queries or *Dialect extension.