Reputation: 21
This is my dao layer:
public class UserDAOImpl extends BaseDAO implements UserDAO {
private PlatformTransactionManager transactionManager;
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
@Override
public int storeSignUp(final StoreSignUp store) throws POSException {
KeyHolder keyHolder = new GeneratedKeyHolder();
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
int id = 0;
try {
this.jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(INSERT_STORE, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, store.getName());
ps.setString(2, store.getZip());
ps.setString(3, store.getCity());
ps.setString(4, store.getState());
ps.setInt(5, store.getCountryCode());
ps.setString(6, store.getAddress());
return ps;
}
}, keyHolder);
store.setId(keyHolder.getKey().intValue());
id = store.getId();
this.jdbcTemplate.update(INSERT_STORE_PHONE,
new Object[] { store.getId(), store.getPhone(), store.getPassword(), store.getSalt() });
transactionManager.commit(status);
} catch (DuplicateKeyException e) {
transactionManager.rollback(status);
}
return id;
}
}
This is my application.xml
<!-- Initialization for TransactionManager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
I get
java.lang.NullPointerException
`com.data.dao.UserDAOImpl.storeSignUp(UserDAOImpl.java:91)`
When i call this method tha data insert into one table but not in another table so i want to be rollback from first table.
Upvotes: 0
Views: 3419
Reputation: 10142
This much should be enough for you - @Transactional(rollbackFor = {Exception.class})
where @Transactional
is org.springframework.transaction.annotation.Transactional
Provided you put in on a public
method , don't eat up Exception
in that public method and also since you are using xml configuration so make sure that you have ,
<tx:annotation-driven transaction-manager="transactionManager" />
where tx
is - xmlns:tx="http://www.springframework.org/schema/tx"
This way you will get rid of explicit commit and rollback boiler plate code around - TransactionDefinition
, TransactionStatus
etc.
This becomes cumbersome if needed in multiple methods.
Upvotes: 1