Spring, SPRINGMVC, and Hibernate integration
The following exception occurred at Sessionfactory.getcurrentsession ()
No Session found for current thread
But using sessionfactory.opensession () is not a problem
Severity: Servlet.service () forservlet [Springdispatcherservlet]inchContext with path [/demo] threw exception [Request processing failed; nested exception isOrg.hibernate.HibernateException:No Session found forCurrent thread] with root causeorg.hibernate.HibernateException:No Session found forCurrent thread at Org.springframework.orm.hibernate4.SpringSessionContext.currentSession ( Springsessioncontext.java:106) at Org.hibernate.Internal. Sessionfactoryimpl.getcurrentsession (Sessionfactoryimpl.java:988) at Com.wzy.dao.BookShopDaoImpl.getAll (Bookshopdaoimpl.java: -) at Com.wzy.services.impl.BookSerImpl.getALL (Bookserimpl.java: +) at Com.wzy.controller.Test.hello (Test.java: $) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at Java.lang.reflect.Method.invo Ke (Unknown Source)
1. What is the difference between getcurrentsession and opensession?
If you are querying, using opensession instead of manually closing, multiple times will cause the connection pool to overflow.
Getcurrentsession is bound to the current thread, and the session is automatically closed when the current thread ends
Getcurrentsession is relatively safe, it is recommended to use
2. Why does the above exception occur when getting getcurrentsession
Because no transactions are configured
Process of Spring Hibernate transaction
Before the method starts, get the session and bind the session to the current thread so that you can use Sessionfactory.getcurrentsession () in the DAO to get the session, and then open the transaction.
If the method ends normally, that is, there is no exception, commit the transaction, dismiss the session with the current thread binding, and close the session.
If the method has an exception, rollback the transaction, unbind, and close the session.
3. How to configure a transaction
The following is the spring configuration file
<?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:tx="Http://www.springframework.org/schema/tx"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><context:component-scanBase-package="Com.wzy"> <context:exclude-filter type="annotation"expression="Org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:db.properties"/> <bean id="DataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="User"Value="${jdbc.user}"></property> <property name="Password"Value="${jdbc.password}"></property> <property name="Driverclass"Value="${jdbc.driverclass}"></property> <property name="Jdbcurl"Value="${jdbc.jdbcurl}"></property> <property name="initialpoolsize"Value="${jdbc.initpoolsize}"></property> <property name="maxpoolsize"Value="${jdbc.maxpoolsize}"></property> </bean> <bean id="sessionfactory" class="Org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="DataSource" ref="DataSource"></property> <property name="mappinglocations"Value="Classpath:com/wzy/entities/*.hbm.xml"></property> <!--<property name="configlocation"Value="Classpath:hibernate.cfg.xml"></property> <property name="hibernateproperties"> <props> <prop key="Hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="Hibernate.show_sql">true</prop> <prop key="Hibernate.format_sql">true</prop> <prop key="Hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!--configuring Spring's declarative Transactions1. Configure Hibernate transaction Manager-<bean id="TransactionManager" class="Org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <!--2. Configure transaction Properties--<tx:advice id="Txadvice"Transaction-manager="TransactionManager"> <tx:attributes> <tx:method name="get*"read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--3. Configure transaction Pointcuts, and then associate transaction properties with transaction Pointcuts<aop:config> <aop:pointcut expression="Execution (* com.wzy.services.*.* (..))"Id="Txpointcut"/> <aop:advisor advice-ref="Txadvice"pointcut-ref="Txpointcut"/> </aop:config> </beans>
4. Why is my spring configuration file already configured, or is that an exception?
If spring, STRUT2, hibernate are integrated, the transaction is configured in spring's configuration file.
However, if it is Spring, SPRINGMVC, hibernate integration, the transaction pointcut should be configured in the SPRINGMVC configuration file
Because spring and SPRINGMVC are two of containers
Just put the <!--in the spring configuration file 3. Configure the transaction pointcut, and then associate the transaction properties with the transaction pointcut -
Put it in the SPRINGMVC configuration file, and the block in spring can be removed.
The following is the configuration file for Springmvc
<?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:mvc="Http://www.springframework.org/schema/mvc"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><context:component-scanBase-package="Com.wzy"></context:component-scan> <!--3. Configure a transaction pointcut, and then associate the transaction properties with the transaction Pointcut-<aop:config> <aop:pointcut expression="Execution (* com.wzy.services.*.* (..))"Id="Txpointcut"/> <aop:advisor advice-ref="Txadvice"pointcut-ref="Txpointcut"/> </aop:config></beans>
in general, when Spring, SPRINGMVC, and Hibernate are integrated
At Sessionfactory.getcurrentsession (), No Session found for the current thread
The transaction needs to be configured and the entry point of the transaction is configured in the SPRINGMVC configuration file
Org.hibernate.HibernateException:No Session found for current thread