如果遇到以上异常的话,恐怕是你的数据源配置的问题了,首先你可以排除一些可能,比如说数据源没问题的,即是DataSource没问题的话,就可以进一步去确定是什么环节出现的问题。刚开始我也是对这个异常感觉到很陌生,很多问题终究还是要一个人去解决。在我重做一遍这个实验之后,我发现这个问题是你的SessionFactory注解时候的一些问题。切确的来说你是要应该理解Session中的一个getCurrentSession() 与openSession()这两个方法的问题。这就是这个问题的实质问题了。重点理解在SSH架构中,spring如何对事务和hibernate
session进行管理
1.getCurrentSession()是必须提交事务的。所以呢,你在用到session.getCurrentSession()的这个方法一定是要配置声明式事务管理。具体的声明式管理可以去网上google一下就知道怎么配置了。
2.openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。
所以简单的解决方法有两种是
1.获得session的时候你就用openSession就行了。
2如果用到getCurrenctSession()的话,你就在sessionFactory 那个bean中配置
<prop key="hibernate.current_session_context_class">
thread
</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</prop>
以下的环节是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ssh.test" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/spring" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.ssh.test.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!--
<prop key="hibernate.current_session_context_class">
thread
</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</prop>
-->
</props>
</property>
</bean>
</beans>
分享到:
相关推荐
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal....
由于项目需求的需要,我们引入了连接池。...我们采用了Hibernate,所以可以考虑hibernate自带的连接池机制,但是发现效率不高,而且Hibernate也推荐使用c3p0或Proxool连接池,在我们的项目中采用了Proxool
Hibernate3技术精辟详解 Hibernate3 Hibernate3详解,看过就知道Hibernate3怎么配置和应用
2、报错:org.hibernate.HibernateException: No Session found for current thread 意思是必须在transcation.isActive()条件下才能执行, 可以解决办法是:当方法不需要事务支持的时候,使用 Session ...
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current ...
18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 19. <property name="dataSource" ref="dataSource" /> 20. <property name="hibernateProperties"> 21. <props> 22...
1.Caused by: org.hibernate.HibernateException: Connection cannot be null when ‘hibernate.dialect’ not set 报错就配置吧 启动项目不报错了,但是访问项目报错了 2.访问报错...
6.8 HibernateException,Checked 6.9 Template/Callback 简化业务方法 6.10 版面:一棵自身循环的树 6.11 版面与帖子:Master/Detail 主从关系 6.12 帖子树及其分页 6.13 与Web层交互 6.14 帖子的多形扩展:...
6.8 HibernateException,Checked 6.9 Template/Callback 简化业务方法 6.10 版面:一棵自身循环的树 6.11 版面与帖子:Master/Detail 主从关系 6.12 帖子树及其分页 6.13 与Web层交互 6.14 帖子的多形扩展:...
例如: 代码如下:HibernateTest.java ... public class HibernateTest { public static void main(String[] args) throws HibernateException { SessionFactory sessionFactory = new Configuration().configure().bui
hibernate详细解析.pdf文档,所有的都有。。
public void executeTransactionBySQL() throws HibernateException { TestJTAByOracleVo vo1=new TestJTAByOracleVo(); vo1.setName("3333333"); HibernateByOracleDAO.addByOracle(vo1); ...
Session session=sessionFactory.openSession(); String hql="from User as u where u.username=? and u.userpass=? and u.userright=?"; Query query=session.createQuery(hql) ; query.setString(0, u....
public List<News> doInHibernate(Session session) throws HibernateException,SQLException { List<News> list = null; Query query = session.createQuery("from News order by id desc"); query....
public void deleteUsers(Users users) throws HibernateException { Session session = HibSessionFactory.currentSession(); Transaction tx = session.beginTransaction(); session.delete(users); tx....