Я использую SpringJdbc.
Как я могу управлять транзакциями в соединении SpringJdbc.
Мой код:
Код: Выделить всё
@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public int addUserRelationshipMapping(final ArrayList userDOs, final long userId) throws UserDataException {
final JdbcTemplate jd = this.getJdbctemplate();
try {
jd.getDataSource().getConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
int totalAdded = 0;
try {
int[] isAdded = jd.batchUpdate(ADD_USER_RELATION_MAPPING, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
final long userRelationId = jd.queryForObject(USER_RELATION_KEY, Long.class);
UserDO userDO = userDOs.get(i);
ps.setLong(1, userRelationId);
ps.setLong(2, userId);
ps.setLong(3, userDO.getprimaryUserId());
ps.setInt(4, 1);
ps.setInt(5, 0);
jd.getDataSource().getConnection().commit();
}
@Override
public int getBatchSize() {
return userDOs.size();
}
});
totalAdded = isAdded.length;
} catch (DuplicateKeyException dExp) {
log.info("error for duplicate key exception ",dExp);
log.error(dExp);
} catch (DataAccessException dExp) {
throw new UserDataException("error while adding user relation for userId is" + userId, dExp);
}
return totalAdded;
}
Поэтому будет использоваться фиксация подключения к базе данных.
Вопрос SOF: запрос Java MYSQL/JDBC возвращает устаревшие данные из кэшированного соединения
Я получил сообщение об ошибке:
Вызвано: java.sql.SQLException: Невозможно вызвать фиксацию, когда autocommit=true
Поэтому мне нужна помощь в этом .
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/284 ... ommitfalse
Мобильная версия