Spring transaction rollback failed

I call the methods of the two Dao layers in the Service layer. One method is used to store a row of Investor data, and one method is used to store Address data with an Investor primary key. The service layer method is as follows. Note that Investor inherits Person, in the same database table.

</property>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    

here is my call to the Service layer method in the UnitTest method.
PersonTest.java

public class PersonTest extends BaseTest {
    
    @Autowired
    private PortfolioService ps;

    
    public PersonTest() {
        System.out.println("PersonTest");
    }
    

    @Test
    public void test8() {
        ps.addInvestor("kk5", "m4", "chrow", "Cua", "Nebska", "D stret");
    }

BaseTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:Spring-Hibernate.xml")
public class BaseTest {
    
    @Before
    public void init() {
        System.out.println("initializing");
    }

}

PersonDaoImpl.java

@Repository
public class PersonDaoImpl implements PersonDao {

    @Autowired
    private SessionFactory sessionFactory;

    public void addAddress(Address ad,int personId) {
        Session session = sessionFactory.openSession();
        //ad.setPersonId(personId);
        session.save(ad);

        session.close();

    }

    public int addPerson(Person p) {
        Session session = sessionFactory.openSession();
        session.save(p);
        int personId = p.getPersonId();
        session.close();
        return personId;
    }



}

Please tell us why this cannot be rolled back. The test method test8 () does not report an error, but the data of Investor is stored in the database and is not rolled back because of the exception of addAddress.


you are two transactions, openSession is to open a new thing, if you want to ensure that all ACID, operations must be in the same transaction

Menu