1

I am novice in spring Transaction Management, I am using it in batch insert but the batch processing is not working, its committing line by line when autoCommit is true, and if autoCommit is false then the data is not inserting in D.B.

below is the insert function.

@Override
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)

public void insertInviteContactMsisdn(final long ettId, final List<String> msisdnList, final List<String> allreadyMsisdnList) {
try {
    String qr = "insert into InviteContactMsisdn (id,ettIdAparty,msisdn,status) values(0,?,?,?)";
    getJdbcTemplate().batchUpdate(qr, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setLong(1, ettId);
            ps.setString(2, msisdnList.get(i));
            ps.setBoolean(3, allreadyMsisdnList.contains(msisdnList.get(i)));
        }
        @Override
        public int getBatchSize() {
            return msisdnList.size();
        }
    });

}catch(DataAccessException dataAccessException) {
     log.error("error in processing insertInviteContactMsisdn ettId:"+ettId);
 throw dataAccessException;
}

}

also the dataSource configuration:-

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.pass}" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
<property name="defaultAutoCommit" value="false" />

Thanks.

I know I am doing something seriously wrong. when I using programmatic Transaction Management the same problem occurring.

@Override
public void insertInviteContactMsisdn(final long ettId, final List<String> msisdnList, final List<String> allreadyMsisdnList) {
// TODO Auto-generated method stub
TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
try {
    String qr = "insert into InviteContactMsisdn (id,ettIdAparty,msisdn,status) values(0,?,?,?)";
    jdbcTemplateObject.batchUpdate(qr, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setLong(1, ettId);
            ps.setString(2, msisdnList.get(i));
            ps.setBoolean(3, allreadyMsisdnList.contains(msisdnList.get(i)));
        }
        @Override
        public int getBatchSize() {
            return msisdnList.size();
        }
    });
    transactionManager.commit(transactionStatus);
}catch(DataAccessException dataAccessException) {
     log.error("error in processing insertInviteContactMsisdn ettId:"+ettId);
 transactionManager.rollback(transactionStatus);
 throw dataAccessException;
}

}

Deepesh Uniyal
  • 923
  • 3
  • 20
  • 44
  • If autoCommit is set to false, then committing the transaction is up to you. if your code doesn't do it, then it's no surprise that you don't see records in the database. – duffymo Feb 12 '16 at 10:24
  • 2
    Only adding `@Transactional` is going to do nothing... You need `` in your configuration. – M. Deinum Feb 12 '16 at 10:25
  • How can I get the connection object for committing the transaction. – Deepesh Uniyal Feb 12 '16 at 10:25
  • You don't as that would beat the whole purpose of declarative transactions wouldn't it. Also you are using MySQL so be aware of [this](http://stackoverflow.com/questions/2993251/jdbc-batch-insert-performance) – M. Deinum Feb 12 '16 at 10:26
  • can u please give me some link for details about . – Deepesh Uniyal Feb 12 '16 at 10:26
  • If adding `` alone doesn't solve the problem, another pitfall may be in the way `insertInviteContactMsisdn()` is called. – Jiri Tousek Feb 12 '16 at 10:29
  • 1
    The [Spring Reference guide](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-annotations). You need a `DataSourceTransactionManager` and `` without those `@Transactional` is only taking up space your source file. – M. Deinum Feb 12 '16 at 10:32

0 Answers0