`
dengyin2000
  • 浏览: 1237220 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

使用Spring AbstractTransactionalDataSourceSpringContextTests测试

阅读更多
在使用AbstractTransactionalDataSourceSpringContextTests这个作为测试hibernate的Service时,遇到这样的一个问题。 就是一定要在代码中显示的调用session.flush()。 更改操作才能立即看到。。

比如

	public void testDeleteBillingEntity(){
		getBillingEntityService().deleteBillingEntity(1);
		assertEquals("BillingEntity(1) should be not existed", getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BILLING_ENTITY WHERE ID=1"), 0);
	}


如果我不在getBillingEntityService().deleteBillingEntity(1) 方法里面调用sesson.flush()的话。 这个测试是失败的。但是在真是环境中的话不调用session.flush()是能行的。Hibernate 默认的是autoFlush 大家应该也遇到相同的问题吧。 是如何解决的?

谢谢
分享到:
评论
9 楼 dengyin2000 2007-04-11  
tsingn 写道
总结一下问题:
     getBillingEntityService().deleteBillingEntity(1);
1)这一步执行后,session(暂称为session1)是否已关闭?若关闭了,session.flush执行了没有?
  应该没有关闭,所以就没有flush
     getCurrentSession().flush();
2)如果session1没有flush,getCurrentSession()取得的session(称为session2)与session1是同一个吗?
从我直接打印(System.out.println(session))来看,应该是同一个session
3)如果说session2和session1不是同一个,那么为什么session2.flush对于session1也起作用了呢?

所以是同一个
8 楼 tsingn 2007-04-11  
总结一下问题:
     getBillingEntityService().deleteBillingEntity(1);
1)这一步执行后,session(暂称为session1)是否已关闭?若关闭了,session.flush执行了没有?    
     getCurrentSession().flush();
2)如果session1没有flush,getCurrentSession()取得的session(称为session2)与session1是同一个吗?
3)如果说session2和session1不是同一个,那么为什么session2.flush对于session1也起作用了呢?
7 楼 dengyin2000 2007-04-11  
Godlikeme 写道
每次调用,应该是取的不同的session,这样是不对的,参见HibernateTemaplate.executeWithSession


我打应了session。 这里取到的session应该是一样的。

这个是SessioinFactoryUtils.getSession(SessionFactory sessionFactory, boolean allowCreate)的javadoc。 里面说了如果allowCreate为false的话  是从current thread中取得
引用

* Get a Hibernate Session for the given SessionFactory. Is aware of and will
* return any existing corresponding Session bound to the current thread, for
* example when using {@link HibernateTransactionManager}. Will create a new
* Session otherwise, if "allowCreate" is <code>true</code>.
* <p>This is the <code>getSession</code> method used by typical data access code,
* in combination with <code>releaseSession</code> called when done with
* the Session. Note that HibernateTemplate allows to write data access code
* without caring about such resource handling.
* @param sessionFactory Hibernate SessionFactory to create the session with
* @param allowCreate whether a non-transactional Session should be created
* when no transactional Session can be found for the current thread
* @return the Hibernate Session
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and
* "allowCreate" is <code>false</code>
* @see #getSession(SessionFactory, Interceptor, SQLExceptionTranslator)
* @see #releaseSession
* @see HibernateTemplate


是不是在一个测试方法中如果service方法调用完了session并没有关掉?  如果关掉的话应该会调用session.flush的
6 楼 tsingn 2007-04-11  
Godlikeme 写道
每次调用,应该是取的不同的session,这样是不对的,参见HibernateTemaplate.executeWithSession
同意!
但是不知道为什么用不同的session flush一下也会起作用?
5 楼 Godlikeme 2007-04-11  
每次调用,应该是取的不同的session,这样是不对的,参见HibernateTemaplate.executeWithSession
4 楼 dengyin2000 2007-04-11  
daquan198163 写道
也遇到过,当时还以为每个方法都应该显示调用session.flush()

SessionFactory的hibernate.jdbc.batch_size属性设成1试试看,我这没有环境暂时试不了


你是在你的Service方法中显示调用flush?  这样不太好吧, 因为在真实环境中是不需要的。 

我上面的方式可以很好的解决在测试的时候遇到的问题
3 楼 daquan198163 2007-04-11  
也遇到过,当时还以为每个方法都应该显示调用session.flush()

SessionFactory的hibernate.jdbc.batch_size属性设成1试试看,我这没有环境暂时试不了
2 楼 dengyin2000 2007-04-11  
ok。 自问自答。 我在BaseTestCase中加了个方法。。

    protected SessionFactory getSessionFactory(){
    	return (SessionFactory) getApplicationContext().getBean("sessionFactory");
    }
    
    protected void flushCurrentSession(){
    	Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
    	if (session !=null){
    		session.flush();
    	}
    }


需要的时候调用下flushCurrentSession()这个方法就ok了
1 楼 dengyin2000 2007-04-11  
http://www.iteye.com/topic/40451

在这篇文章里面说了要显示的调用session.flush()

感到奇怪的是当deleteBillingEntity 方法执行完之后, session应该会close呀。 那么如果

    public void testDeleteBillingEntity(){  
        getBillingEntityService().deleteBillingEntity(1);  
        getCurrentSession().flush();
        assertEquals("BillingEntity(1) should be not existed", getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BILLING_ENTITY WHERE ID=1"), 0);  
   }  


如果这样的话 能拿到在deleteBillingEntity 方面里面得session么?

相关推荐

    测试spring的存储过程

    在实际操作中,我们可能需要创建一个测试类,继承Spring的AbstractTransactionalDataSourceSpringContextTests或类似的基类,这样可以在每个测试方法前后自动处理事务。在测试方法中,使用Spring提供的JdbcTemplate...

    spring与dbunit集成测试

    1. **配置Spring测试环境**:使用Spring Test模块,创建一个继承自`AbstractTransactionalDataSourceSpringContextTests`或`AbstractTransactionalJUnit4SpringContextTests`的测试类。这两个类提供了事务管理,确保...

    Spring 2.5 TestContext

    - `@RunWith(SpringJUnit4ClassRunner.class)`:这是JUnit测试类必须的注解,它告诉JUnit使用Spring提供的测试运行器来执行测试。 - `@ContextConfiguration`:用于指定配置文件的位置或加载ApplicationContext的...

    主题:在Spring中结合Dbunit对Dao进行集成单元测试

    在Spring项目中,我们需要创建一个测试配置类,该类通常继承自`AbstractTransactionalDataSourceSpringContextTests`或`AbstractTransactionalJUnit4SpringContextTests`,这两个类提供了事务管理和数据源的自动...

    使用junit测试ssh中的dao

    2. **创建测试类**:创建一个继承自Spring的AbstractTransactionalDataSourceSpringContextTests的测试类,这个类提供了事务管理,并自动回滚事务,防止测试对数据库产生副作用。 3. **注入依赖**:使用Spring的@...

    022-preload-database-execute-sql-spring-testing

    Spring还提供了如`AbstractTransactionalDataSourceSpringContextTests`和`AbstractTransactionalJUnit4SpringContextTests`这样的基类,它们提供了自动事务管理和数据库清理的功能,简化了数据库测试的编写。...

    Spring整合Hibernate案例

    - Spring提供了`AbstractTransactionalDataSourceSpringContextTests`基类,用于创建基于Spring的事务测试环境,方便对整合后的代码进行单元测试。 6. **优点** - **解耦**:通过Spring管理Hibernate的...

Global site tag (gtag.js) - Google Analytics