Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:56:24+00:00 2026-05-29T09:56:24+00:00

I’m having problems with Spring transactions. I really need help as I can’t figure

  • 0

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?

My Eclipse project is available for download at http://www52.zippyshare.com/v/4142091/file.html. All dependencies are there so it’s easy to get going.

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<Person> getPersons() {
        Map<String, Object> namedParameters = new HashMap<String, Object>();
        String getCustomers = "SELECT name FROM " + tableName + " ORDER BY name ASC";
        return namedParameterJdbcTemplate.query(getCustomers, namedParameters, getRowMapper());
    }

    public void createPersons(List<Person> customers) {
        SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(customers.toArray());
        String createCustomer = "INSERT INTO " + tableName + " VALUES(:name)";
        namedParameterJdbcTemplate.batchUpdate(createCustomer, params);
    }

    public void deleteCustomers() {
        Map<String, Object> namedParameters = new HashMap<String, Object>();
        String deleteCustomers = "DELETE FROM " + tableName;
        namedParameterJdbcTemplate.update(deleteCustomers, namedParameters);
    }

    private static RowMapper<Person> getRowMapper() {
        return new RowMapper<Person>() {
            @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());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:56:25+00:00Added an answer on May 29, 2026 at 9:56 am

    @Anders Your InnerClass with the @Transactional annotation does not derive from an interface, if you are not using AspectJ weaving or CG-LIB based proxies, the @Transactional aspect won’t take effect, as the dynamic proxies require an interface to be present. A quick fix will be to derive your inner class from an interface, define the bean in the spring config and consistently use the interface for referring to the bean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.