Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring transaction issue
    primarykey
    data
    text
    <p>I'm having problems with Spring transactions. I really need help as I can't figure out why personsDao2 is not rolled back as should (see assert below commented with "FAILS!"). Any input?</p> <p>My Eclipse project is available for download at <a href="http://www52.zippyshare.com/v/4142091/file.html" rel="nofollow">http://www52.zippyshare.com/v/4142091/file.html</a>. All dependencies are there so it's easy to get going.</p> <pre><code>import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.google.common.collect.Lists; public class MyInnerClass { private PersonsDao personsDao; public MyInnerClass() { } public PersonsDao getPersonsDao() { return personsDao; } public void setPersonsDao(PersonsDao personsDao) { this.personsDao = personsDao; } @Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = Exception.class) public void method() { personsDao.createPersons(Lists.newArrayList(new Person("Eva"))); } } import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.google.common.collect.Lists; public class MyOuterClass { private MyInnerClass myInnerClass; private PersonsDao personsDao; public MyInnerClass getMyInnerClass() { return myInnerClass; } public void setMyInnerClass(MyInnerClass myInnerClass) { this.myInnerClass = myInnerClass; } public void setMyInnerClass() { } public PersonsDao getPersonsDao() { return personsDao; } public void setPersonsDao(PersonsDao personsDao) { this.personsDao = personsDao; } public MyOuterClass() { } @Transactional(propagation = Propagation.REQUIRED, rollbackFor=Exception.class) public void method() { try { personsDao.createPersons(Lists.newArrayList(new Person("Adam"))); throw new RuntimeException("Forced rollback"); } finally { myInnerClass.method(); } } } public class Person { public Person(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return "Customer [name=" + name + "]"; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } private String name; } import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; public class PersonsDao { public PersonsDao(DataSource dataSource, String tableName) { namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); this.tableName = tableName; } public List&lt;Person&gt; getPersons() { Map&lt;String, Object&gt; namedParameters = new HashMap&lt;String, Object&gt;(); String getCustomers = "SELECT name FROM " + tableName + " ORDER BY name ASC"; return namedParameterJdbcTemplate.query(getCustomers, namedParameters, getRowMapper()); } public void createPersons(List&lt;Person&gt; customers) { SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(customers.toArray()); String createCustomer = "INSERT INTO " + tableName + " VALUES(:name)"; namedParameterJdbcTemplate.batchUpdate(createCustomer, params); } public void deleteCustomers() { Map&lt;String, Object&gt; namedParameters = new HashMap&lt;String, Object&gt;(); String deleteCustomers = "DELETE FROM " + tableName; namedParameterJdbcTemplate.update(deleteCustomers, namedParameters); } private static RowMapper&lt;Person&gt; getRowMapper() { return new RowMapper&lt;Person&gt;() { @Override public Person mapRow(ResultSet arg0, int arg1) throws SQLException { return new Person(arg0.getString("name")); } }; } private NamedParameterJdbcTemplate namedParameterJdbcTemplate; private String tableName; } import static org.junit.Assert.*; import javax.annotation.Resource; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/beans.xml") @Transactional(rollbackFor = Exception.class) public class PersonsDaoTest { @Resource private MyInnerClass myInnerClass; @Resource private MyOuterClass myOuterClass; @Test(expected = Exception.class) public void test() { myOuterClass.method(); fail(); } @After public void after() { assertEquals(1, myInnerClass.getPersonsDao().getPersons().size()); assertEquals(0, myOuterClass.getPersonsDao().getPersons().size()); } @Before public void before() { myInnerClass.getPersonsDao().deleteCustomers(); myOuterClass.getPersonsDao().deleteCustomers(); assertEquals(0, myInnerClass.getPersonsDao().getPersons().size()); assertEquals(0, myOuterClass.getPersonsDao().getPersons().size()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload