The problem is that you are not using the connection managed by spring, instead you are opening a new connection. Change the code to the following and try.
import org.springframework.jdbc.datasource.DataSourceUtils;
@Transactional
public void getData() {
Connection con=null;
try {
// Get the connection associated with the transaction
con = DataSourceUtils.getConnection(dataSource);
Statement stat = con.createStatement();
stat.executeUpdate("update testdata set foo=9 where id=1");
throw new RuntimeException("an Exception for test");
} catch (SQLException e) {
e.printStackTrace();
} finally{
DataSourceUtils.releaseConnection(dataSource, con);
}
}
If you are writing new code you should be using the JdbcTemplate instead of raw jdbc.
class HelloService {
JdbcTemplate jdbcTemplate;
public setDataSource(DataSource dataSource) {
jdbcTemplate = new JDBCTemplate(dataSource);
}
@Transactional
public void getData() {
jdbcTemplate.update(update testdata set foo=9 where id=1);
throw new RuntimeException("an Exception for test");
}