论坛首页 Java企业应用论坛

使用Spring AbstractTransactionalDataSourceSpringContextTests测试

浏览 13683 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-11  
在使用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 大家应该也遇到相同的问题吧。 是如何解决的?

谢谢
   发表时间: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么?
0 请登录后投票
   发表时间: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了
0 请登录后投票
   发表时间:2007-04-11  
也遇到过,当时还以为每个方法都应该显示调用session.flush()

SessionFactory的hibernate.jdbc.batch_size属性设成1试试看,我这没有环境暂时试不了
0 请登录后投票
   发表时间:2007-04-11  
daquan198163 写道
也遇到过,当时还以为每个方法都应该显示调用session.flush()

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


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

我上面的方式可以很好的解决在测试的时候遇到的问题
0 请登录后投票
   发表时间:2007-04-11  
每次调用,应该是取的不同的session,这样是不对的,参见HibernateTemaplate.executeWithSession
0 请登录后投票
   发表时间:2007-04-11  
Godlikeme 写道
每次调用,应该是取的不同的session,这样是不对的,参见HibernateTemaplate.executeWithSession
同意!
但是不知道为什么用不同的session flush一下也会起作用?
0 请登录后投票
   发表时间: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的
0 请登录后投票
   发表时间: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也起作用了呢?
0 请登录后投票
   发表时间: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也起作用了呢?

所以是同一个
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics