首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring + hibernate时,current_session_context_class配置有关问题

2012-10-31 
spring + hibernate时,current_session_context_class配置问题当初学使用spring来配置hibernate的事务管理

spring + hibernate时,current_session_context_class配置问题

当初学使用spring来配置hibernate的事务管理时,当要把current session绑定到线程上,本人很习惯式的把hibernate的current_session_context_class的值设置为thread(hibenate native api 提供三个值:thread,jta, manage).

可是当调用session.save()方法是遇到以下的错:save is not valid without transaction.

?

applicationContext.xml

<!-- ORM的sessionFactory -->
??? <bean id="sessionFactory"
??? ??? value="classpath:hibernate.cfg.xml" />
??? ??? <property name="configurationClass" value="org.hibernate.cfg.Configuration" />
??? </bean>
???
??? <tx:annotation-driven/>
??? <!-- 配置事务处理意见 -->
??? <tx:advice id="baseAdvice" transaction-manager="txManager">
??? ??? <!-- 事务的属性 -->
??? ??? <tx:attributes>
??? ??? ??? <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
??? ??? ??? <tx:method name="*"/>
??? ??? </tx:attributes>
??? </tx:advice>

??? <!-- 配置事务的aop -->
??? <aop:config>
??? ??? <aop:pointcut id="daoPointcut" expression="execution(* com.mcao.dao.*.*(..))" />
??? ??? <aop:advisor advice-ref="baseAdvice" pointcut-ref="daoPointcut" />
??? </aop:config>
???
??? <!-- 定义事务管理器 -->
??? <bean id="txManager"
??? ??? ref="sessionFactory" />
??? </bean>
???
??? <bean id="userDao" ref="sessionFactory" />
??? </bean>

??? <bean id="userService" ref="userDao" />
??? </bean>

?

hibernate.cfg.xml

<session-factory>
??? ???
??? ??? <!-- mysql jdbc 连接配置 -->
??? ??? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??? ??? <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
??? ??? <property name="connection.username">root</property>
??? ??? <property name="connection.password">123456</property>
??? ???

??? ??? <!-- JDBC connection pool (use the built-in) -->
??? ??? <property name="connection.pool_size">1</property>

??? ??? <!-- SQL dialect -->
??? ??? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

??? ??? <!-- Enable Hibernate's automatic session context management -->
??? ??? <property name="current_session_context_class">thread</property>

??? ??? <!-- Disable the second-level cache -->
??? ??? <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

??? ??? <!-- Echo all executed SQL to stdout -->
??? ??? <property name="show_sql">true</property>
??? ???
??? ??? <!-- batch size -->
??? ??? <property name="hibernate.jdbc.batch_size">40</property>

??? ??? <!-- Drop and re-create the database schema on startup -->
??? ??? <property name="hbm2ddl.auto">update</property>

??? ??? <mapping + method.getName() + "] in non-transacted context" );
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else if ( "reconnect".equals( method.getName() )
??? ??? ??? ??? ??? ????????? || "disconnect".equals( method.getName() ) ) {
??? ??? ??? ??? ??? ??? // allow these (deprecated) methods to pass through
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else {
??? ??? ??? ??? ??? ??? throw new HibernateException( method.getName() + " is not valid without active transaction" );
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? ??? log.trace( "allowing proxied method [" + method.getName() + "] to proceed to real session" );
??? ??? ??? ??? return method.invoke( realSession, args );
??? ??? ??? }
??? ??? ??? catch ( InvocationTargetException e ) {
??? ??? ??? ??? if ( e.getTargetException() instanceof RuntimeException ) {
??? ??? ??? ??? ??? throw ( RuntimeException ) e.getTargetException();
??? ??? ??? ??? }
??? ??? ??? ??? else {
??? ??? ??? ??? ??? throw e;
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }

?

解决方法有二:

1, 添加session.beginTransaction(), 开启事务。

2, current_session_context_class对应的class设置为org.springframework.orm.hibernate3.SpringSessionContext
或者不指定(同样是org.springframework.orm.hibernate3.SpringSessionContext),在此类中,获取current session时,就已经把事务开启了。

?

?

热点排行