Results 1 to 6 of 6

Thread: confusing Propagation.REQUIRED problem

  1. #1
    Join Date
    Jan 2008
    Posts
    25

    Default confusing Propagation.REQUIRED problem

    Hello
    rollback does not work
    I use AbstractTransactionalJUnit4SpringContextTests for my JUnit tests

    hier is my service:
    @Transactional(propagation = Propagation.REQUIRED)
    public void registerFreelancer(Freelancer aFreelancer)

    It seems that I open a internal transaction, which write data in database
    so the test does not a rollback about this data in the servicetransaction

    if i do this:
    @Transactional(propagation = Propagation.SUPPORTS)

    then its work, the data is not in the database after the test

    but i dont understand Propagation.REQUIRED should take the parenttransaction
    and do a rollback after the test.. or not?

    can anybody clear my view?

  2. #2
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Are you sure that parent transaction exists?
    The behavior of PROPAGATION_REQUIRED is
    • Join active transaction if any
    • Otherwise create new transaction

    The behavior of PROPAGATION_SUPPORTS is
    • Join active transaction if any
    • Otherwise run without transaction


    Quote Originally Posted by taubek View Post
    Hello
    rollback does not work
    I use AbstractTransactionalJUnit4SpringContextTests for my JUnit tests

    hier is my service:
    @Transactional(propagation = Propagation.REQUIRED)
    public void registerFreelancer(Freelancer aFreelancer)

    It seems that I open a internal transaction, which write data in database
    so the test does not a rollback about this data in the servicetransaction

    if i do this:
    @Transactional(propagation = Propagation.SUPPORTS)

    then its work, the data is not in the database after the test

    but i dont understand Propagation.REQUIRED should take the parenttransaction
    and do a rollback after the test.. or not?

    can anybody clear my view?

  3. #3
    Join Date
    Jan 2008
    Posts
    25

    Default

    Quote Originally Posted by al0 View Post
    Are you sure that parent transaction exists?
    The behavior of PROPAGATION_REQUIRED is
    ähm
    no i am not ;-)
    I thought about this, because there was no entries in the database
    with PROPAGATION.SUPPORT
    after test execution, so I thought there is an transaction with rollback.
    But I found out, that there is no insert entry in the log, therefore the
    test write no entry in the database.
    So I think there is no parrent transaction.

    But when there is no parent transaction,
    and I am use PROPAGATION_REQUIRED,
    than it should create one and do a rollback after this transaction.

    in the log I have an entry "Rolled back transaction..."
    but the Data is in the database...

    anyone an idea?

  4. #4
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Sorry, there is not enough information.

    May you post test code and pertinent part of configuration along with the log (as attachment if they are to big for post).

    Try as well to activate debug-level logging for Spring if you have not done so yet.

    Regards,

    Oleksandr
    Quote Originally Posted by taubek View Post
    ähm
    no i am not ;-)
    I thought about this, because there was no entries in the database
    with PROPAGATION.SUPPORT
    after test execution, so I thought there is an transaction with rollback.
    But I found out, that there is no insert entry in the log, therefore the
    test write no entry in the database.
    So I think there is no parrent transaction.

    But when there is no parent transaction,
    and I am use PROPAGATION_REQUIRED,
    than it should create one and do a rollback after this transaction.

    in the log I have an entry "Rolled back transaction..."
    but the Data is in the database...

    anyone an idea?

  5. #5
    Join Date
    Jan 2008
    Posts
    25

    Default

    ok.. here more details:

    My base test class:
    Code:
    @ContextConfiguration(locations = {"classpath:f4fApplicationContext.xml"})
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    public class BaseTxTest extends AbstractTransactionalJUnit4SpringContextTests {
    	
    	@Autowired
    	private SessionFactory sessionFactory;
    
    	protected void flushAndClearSession() {
    		sessionFactory.getCurrentSession().flush();
    		sessionFactory.getCurrentSession().clear();
    	}
    }
    my test class:
    Code:
    public class FreelancerServiceTest extends BaseTxTest {
    
    	private F4FServiceAccessor f4fServiceAccessor;
    	private FreelancerService freelancerService;
    	private F4FDaoAccessor f4fDaoAccessor;
    
    	@Before
    	public void setUp() {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    				"f4fApplicationContext.xml");
    		f4fServiceAccessor = (F4FServiceAccessor) context
    				.getBean(F4FServiceAccessor.BEAN_NAME);
    		f4fDaoAccessor = (F4FDaoAccessor) context
    				.getBean(F4FDaoAccessor.BEAN_NAME);
    		freelancerService = f4fServiceAccessor.getFreelancerService();
    	}
    
    	@Test
    	public void testRegisterFreelancer() {
    		Freelancer freelancer = f4fDaoAccessor.getFreelancerDao().instanciate();
    		Activity activity = f4fDaoAccessor.getActivityDao().instanciate();
    
    		Skill skill = f4fDaoAccessor.getSkillDao().instanciate();
    
    		freelancer.addActivity(activity);
    		freelancer.addSkill(skill);
    
    		freelancerService.registerFreelancer(freelancer);
    	}
    }
    my service method:
    Code:
    	@Transactional(propagation = Propagation.REQUIRED)
    	public void registerFreelancer(Freelancer aFreelancer) {
    		f4fDaoAccessor.getFreelancerDao().save(aFreelancer);
    		LOGGER.debug("Freelancer saved: "+aFreelancer);
    	}
    my transaction context:
    Code:
    <!--
    		Transaction context
    	-->
    	
    	<!-- enable the configuration of transactional behavior based on annotations -->
    	<tx:annotation-driven transaction-manager="transactionManager"
    		proxy-target-class="true" />
    
    	<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA)-->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    my session factory bean:
    Code:
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="configLocations">
    			<list>
    				<value>classpath:hibernate.cfg.xml</value>
    			</list>
    		</property>
    	</bean>
    my hibernate config:
    Code:
    <hibernate-configuration>
    	<session-factory>
    		<property name="hibernate.show_sql">true</property>
    		<property name="hibernate.format_sql">false</property>
    		<property name="hibernate.use_sql_comments">false</property>
    		<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    		<property name="hibernate.hbm2ddl.auto">none</property>
    
    		<!-- F4F -->
    		<mapping class="dtp.f4f.bo.Freelancer" />
    		<mapping class="dtp.f4f.bo.Skill" />
    		<mapping class="dtp.f4f.bo.Activity" />
    	</session-factory>
    </hibernate-configuration>
    my datasource:
    Code:
    <bean id="dataSource"
    		class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:oracle:thin:@192.168.27.129:1521:XE</value>
    		</property>
    		<property name="defaultAutoCommit">
    			<value>false</value>
    		</property>
    		<property name="username">
    			<value>f4f</value>
    		</property>
    		<property name="password">
    			<value>f4f</value>
    		</property>
    	</bean>
    a part from the log:
    Code:
    DEBUG SQL - insert into FREELANCERS (birthday, city, country, firstname, lastname, street, teleBusines, telePrivate, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    Hibernate: insert into FREELANCERS (birthday, city, country, firstname, lastname, street, teleBusines, telePrivate, zipcode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    DEBUG AbstractBatcher - Executing batch size: 1
    DEBUG Expectations - success of batch update unknown: 0
    DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG SQL - insert into ACTIVITIES (company, description, time, id) values (?, ?, ?, ?)
    Hibernate: insert into ACTIVITIES (company, description, time, id) values (?, ?, ?, ?)
    DEBUG AbstractBatcher - Executing batch size: 1
    DEBUG Expectations - success of batch update unknown: 0
    DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG SQL - insert into SKILLS (description, overview, time, id) values (?, ?, ?, ?)
    Hibernate: insert into SKILLS (description, overview, time, id) values (?, ?, ?, ?)
    DEBUG AbstractBatcher - Executing batch size: 1
    DEBUG Expectations - success of batch update unknown: 0
    DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG AbstractCollectionPersister - Inserting collection: [dtp.f4f.bo.Freelancer.activities#57]
    DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG SQL - update ACTIVITIES set FREELANCER_ID=?, ORDERNUMBER=? where id=?
    Hibernate: update ACTIVITIES set FREELANCER_ID=?, ORDERNUMBER=? where id=?
    DEBUG AbstractCollectionPersister - done inserting collection: 1 rows inserted
    DEBUG AbstractCollectionPersister - Inserting collection: [dtp.f4f.bo.Freelancer.skills#57]
    DEBUG AbstractBatcher - Executing batch size: 1
    DEBUG Expectations - success of batch update unknown: 0
    DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG SQL - update SKILLS set FREELANCER_ID=? where id=?
    Hibernate: update SKILLS set FREELANCER_ID=? where id=?
    DEBUG AbstractCollectionPersister - done inserting collection: 1 rows inserted
    DEBUG AbstractBatcher - Executing batch size: 1
    DEBUG Expectations - success of batch update unknown: 0
    DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG JDBCTransaction - committed JDBC Connection
    DEBUG ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    DEBUG ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    DEBUG ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    DEBUG JDBCTransaction - rollback
    DEBUG JDBCTransaction - rolled back JDBC Connection
    DEBUG ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    DEBUG ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    DEBUG ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    INFO  TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [[TestContext@eec137 testClass = FreelancerServiceTest, locations = array<String>['classpath:f4fApplicationContext.xml'], testInstance = dtp.f4f.service.FreelancerServiceTest@70f15e, testMethod = testRegisterFreelancer@FreelancerServiceTest, testException = [null]]].
    INFO  GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@43da1b: display name [org.springframework.context.support.GenericApplicationContext@43da1b]; startup date [Tue Apr 22 12:41:05 CEST 2008]; root of context hierarchy
    INFO  DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@857066: defining beans [sessionFactory,dataSource,f4fDaoAccessor,freelancerDao,skillDao,activityDao,f4fServiceAccessor,freelancerService,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
    INFO  AnnotationSessionFactoryBean - Closing Hibernate SessionFactory
    INFO  SessionFactoryImpl - closing

  6. #6
    Join Date
    Jan 2008
    Posts
    25

    Default

    ok I got it!!!

    I load the applicationContext a second time in the setUp methode

    Code:
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    				"f4fApplicationContext.xml");
    that was the failure, I found out that i can access to the applicationContext through the AbstractTransactionalJUnit4SpringContextTests class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •