解决方案 »
- 请问,java.lang.StackOverflowError怎么个处理,什么原因?
- 用wps的帮我一下
- Hibernate+Struts+spring整合问题
- IE版本访问和页面的问题
- weblogic10.0.3解决和hibernate3类冲突
- 谢谢!!还想问一下,怎样用java程序实现对oracle数据clob类型的存取操作!!谢谢,在线等待!!
- 请问我怎样才能知道现在tomcat中有多少激活的数据库连接在连接池中?
- 在jb8中建cmp时老是把它自动新建一个包,想重新设置都不行,烦死了,怎么设置的
- spring实例化和使用new
- 有经验的大大请进来看下。。。
- 如何得到客户端的IP
- 问一个关于Spring的问题,请高手详细解答下。
你可以使用getClass方法判断你当前的Session是从什么地方创建的 通过Spring创建的和通过HibernateSessionFactory创建的返回的信息是不一样的至于需要不需要关闭这个我不知道 因为我没用过
但是你自己拿session操作, 当然要关了
类似这样的代码都需要手动关闭的Session session = this.getSession();
Query query = session.createQuery(hql);
query.setMaxResults(2);
List<?> cs=query.list();如果使用getHibernateTemplate().save(entity);getHibernateTemplate().update(entity);不用手动关闭的
而这样的代码不用手动关闭public void save(DBComment commnet,String hql) {
DBTelevision television=(DBTelevision) this.getHibernateTemplate().find(hql).get(0);
commnet.setTeleName(television.getName());
television.setCommentNum(television.getCommentNum()+1);
this.getHibernateTemplate().save(commnet);
this.getHibernateTemplate().update(television);
}
希望对你有帮助
祝你好运
<property name="jndiName" value="java:comp/env/jdbc/OracleDB" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
...
</props>
</property>
<property name="mappingResources">
<list>
<value>com/.../hbm/User.hbm.xml</value>
...
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
...
</props>
</property>
</bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributeSource" ref="transactionAttributeSource" />
</bean> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>userManager</value>
...
</list>
</property>
</bean> public Session getSession() {
return SessionFactoryUtils.getSession(this.sessionFactory, true);
}
...
public List getUserList() {
String hql = "from UserInfo where userId=?";
return this.getSession().createQuery(hql).list();
}
平时我这样使用Session,并没有对Session进行任何管理。也没有出现过任何问题。
-----------------------------------------------------------------------------------
今天观察了spring的源代码,发现HibernateTransactionManager的里面有这个方法:
protected void doCleanupAfterCompletion(Object transaction)
Cleanup resources after transaction completion. Called after doCommit and doRollback execution 这是doCleanupAfterCompletion方法其中一段源码:可以看出有对Session进行管理的。所以我认为加入了事务管理以后不需要对Session进行关闭等操作是没有问题的。
Session session = txObject.getSessionHolder().getSession();
if (txObject.isNewSessionHolder()) {
if (logger.isDebugEnabled()) {
logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
"] after transaction");
}
SessionFactoryUtils.releaseSession(session, getSessionFactory());
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Not closing pre-bound Hibernate Session [" +
SessionFactoryUtils.toString(session) + "] after transaction");
}
if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
}
}
像这种获取session进行操作的话,如果有事务,不用关闭。关闭反而会有其它问题。例如嵌套事务中会出现session已关闭异常。如果没有事务管理,那么就一定要关闭session。
xyz20003:项目使用了“事务管理”,通过HibernateDaoSupport.getSession()或者SessionFactoryUtils.getSession(sessionFactory, true)得到Session并使用的时候,不需要对Session关闭。但是使用SessionFactory.openSession()得到Session并使用的时候就要手动关闭,是不是?或者讲下原因。谢谢了!
finally{
if(session!=null)
if(session.isOpen())
session.close();
}
Configuration config = new Configuration();
config.configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(gb);
tx.commit();
他启动的事物是有手动关闭的,可能它的方法跟你讲的方法有点不同,但大家一起学习学习!
一、首先项目使用了事务管理HibernateTransactionManager
二、继承HibernateDaoSupport,直接使用HibernateTemplate的save,find,update等方法,大家都已经知道此时由spring来维护Session,不需要我们再程序中手动维护Session。原因就不用说了,相信看过spring源码的都非常清楚。
三、现在的疑问:在依然使用事务管理HibernateTransactionManager的前提下,通过<1>HibernateDaoSupport.getSession()
<2>SessionFactoryUtils.getSession(sessionFactory, true)
<3>SessionFactory.openSession()
以上3种任意一种方式得到Session,然后使用Session的save,update,createQuery(hql).list()等方法。之后是否需要手动关闭Session?原因?请知道答案的高手指教。其他不相关的、简单的大家都知道的就不需要说了。呵呵