Code Bye

java 执行sessionFactory.getCurrentSession()报错

sessionFactory.getCurrentSession()
执行到这局的时候报错Unable to locate current JTA transaction
org.hibernate.HibernateException: Unable to locate current JTA transaction

事务文件:

<?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:tx=”http://www.springframework.org/schema/tx”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
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/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           ”
default-autowire=”byName”>

    <!– 开启AOP监听 只对当前配置文件有效 –>
<aop:aspectj-autoproxy expose-proxy=”true”/>

<!– 开启注解事务 只对当前配置文件有效 –>
   <tx:annotation-driven transaction-manager=”txManager”/>
<!–<tx:jta-transaction-manager /> 配置Spring使用JTA事务 –>

    <bean id=”txManager” class=”org.springframework.orm.hibernate4.HibernateTransactionManager”>
        <property name=”sessionFactory” ref=”sessionFactory”/>
    </bean>

    <tx:advice id=”txAdvice” transaction-manager=”txManager”>
        <tx:attributes>
            <tx:method name=”save*” propagation=”REQUIRED” />
            <tx:method name=”add*” propagation=”REQUIRED” />
            <tx:method name=”create*” propagation=”REQUIRED” />
            <tx:method name=”insert*” propagation=”REQUIRED” />
            <tx:method name=”update*” propagation=”REQUIRED” />
            <tx:method name=”merge*” propagation=”REQUIRED” />
            <tx:method name=”del*” propagation=”REQUIRED” />
            <tx:method name=”remove*” propagation=”REQUIRED” />
            <tx:method name=”put*” propagation=”REQUIRED” />
            <tx:method name=”use*” propagation=”REQUIRED”/>
            <!–hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到–>
            <tx:method name=”get*” propagation=”REQUIRED” read-only=”false” />
            <tx:method name=”count*” propagation=”REQUIRED” read-only=”false” />
            <tx:method name=”find*” propagation=”REQUIRED” read-only=”false” />
            <tx:method name=”list*” propagation=”REQUIRED” read-only=”false” />
            <tx:method name=”*” read-only=”false” />
        </tx:attributes>
    </tx:advice>
    
    <aop:config expose-proxy=”true”>
        <!– 只对业务逻辑层实施事务 –>
        <aop:pointcut id=”txPointcut” expression=”execution(* com.dao.impl.*.*(..))” />
        <aop:advisor advice-ref=”txAdvice” pointcut-ref=”txPointcut”/>
    </aop:config>

</beans>

spring文件贴出部分代码:
<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>${hibernate.dialect}</prop>
<prop key=”hibernate.show_sql”>${hibernate.show_sql}</prop>
<prop key=”hibernate.format_sql”>true</prop>
<prop key=”hibernate.query.substitutions”>${hibernate.query.substitutions}</prop>
<prop key=”hibernate.default_batch_fetch_size”>${hibernate.default_batch_fetch_size}</prop>
<prop key=”hibernate.max_fetch_depth”>${hibernate.max_fetch_depth}</prop>
<prop key=”hibernate.generate_statistics”>${hibernate.generate_statistics}</prop>
<prop key=”hibernate.bytecode.use_reflection_optimizer”>${hibernate.bytecode.use_reflection_optimizer}</prop>
</props>
</property>

<bean id=”BaseDaoImp” class=”com.dao.BaseDaoImp”>
<property name=”sessionFactory” ref=”sessionFactory”>
</property>
</bean>

请哪位大侠教教 谢谢


20分
sessionFactory 用的是org.springframework.orm.hibernate4.LocalSessionFactoryBean 么?

是的话在hibernateProperties中加个配置:
<prop key=”hibernate.current_session_context_class”>
                        org.springframework.orm.hibernate4.SpringSessionContext
                    </prop>


10分
楼主参考一下http://www.blogjava.net/javababy/archive/2006/05/28/48549.aspx
引用 1 楼 whos2002110 的回复:

sessionFactory 用的是org.springframework.orm.hibernate4.LocalSessionFactoryBean 么?

是的话在hibernateProperties中加个配置:
<prop key=”hibernate.current_session_context_class”>
                        org.springframework.orm.hibernate4.SpringSessionContext
                    </prop>

这个试过了 ,加上了也还是抱着个错误

什么情况  没人知道吗?

50分
你的 sessionFactory 在哪里?
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://192.168.1.121:3306/xsl"></property>
		<property name="username" value="root"></property>
		<property name="password" value="sa"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="annotatedClasses">
			<list>
			    
				<value>org.birdy.entity.Media</value>
			    
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.query.factory_class">
					  org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.query.substitutions">
					true ""T"", false ""F""
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>  
				<prop key="hibernate.current_session_context_class">
					org.springframework.orm.hibernate4.SpringSessionContext
				</prop>

			</props>
		</property>
	</bean>

<?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:tx=”http://www.springframework.org/schema/tx”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans    
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           ”
default-autowire=”byName”>

<!– auto register Processor –>
<context:annotation-config />

<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
<property name=”driverClassName” value=”${hotel.dataSource.driverClassName}”/>
<property name=”url” value=”${hotel.dataSource.url}”/>
<property name=”username” value=”${hotel.dataSource.username}”/>
<property name=”password” value=”${hotel.dataSource.password}”/>
<property name=”initialSize” value=”5″/>
<property name=”maxActive” value=”30″/>
<property name=”maxWait” value=”60000″/>
<property name=”maxIdle” value=”10″/>
<property name=”minIdle” value=”5″/>
<property name=”removeAbandoned” value=”true”/>
<property name=”removeAbandonedTimeout” value=”180″/>
<property name=”testWhileIdle” value=”true”/>
<property name=”testOnBorrow” value=”false”/>
<property name=”testOnReturn” value=”false”/>
<property name=”timeBetweenEvictionRunsMillis” value=”30000″/>
<property name=”numTestsPerEvictionRun” value=”20″/>
</bean>

<!– hibernet –>

<bean id=”sessionFactory” class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”
>
<property name=”dataSource” ref=”dataSource”/>

<property name=”mappingResources”>

<list>
<value>com/model/teacher.hbm.xml</value>
<value>com/model/teacherCourse.hbm.xml</value>
<value>com/model/studentcourseteacher.hbm.xml</value>
<value>com/model/studentGrade.xml</value>
<value>com/model/Student.hbm.xml</value>

</list>
</property>

<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>${hibernate.dialect}</prop>
<prop key=”hibernate.show_sql”>${hibernate.show_sql}</prop>
<prop key=”hibernate.format_sql”>true</prop>
<prop key=”hibernate.query.substitutions”>${hibernate.query.substitutions}</prop>
<prop key=”hibernate.default_batch_fetch_size”>${hibernate.default_batch_fetch_size}</prop>
<prop key=”hibernate.max_fetch_depth”>${hibernate.max_fetch_depth}</prop>
<prop key=”hibernate.generate_statistics”>${hibernate.generate_statistics}</prop>
<prop key=”hibernate.bytecode.use_reflection_optimizer”>${hibernate.bytecode.use_reflection_optimizer}</prop>
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>
    <prop key=”hibernate.connection.autocommit”>true</prop>

</props>
</property>
</bean>

<bean id=”hibernateTemplate” class=”org.springframework.orm.hibernate4.HibernateTemplate”>
<property name=”sessionFactory” ref=”sessionFactory”>
</property>
</bean>

<bean id=”BaseDaoImp” class=”com.dao.BaseDaoImp”>
<property name=”sessionFactory” ref=”sessionFactory”></property>
<property name=”hibernateTemplate” ref=”hibernateTemplate”> </property>
</bean>
</beans>

基本上我都加了啊  为啥还报这个错误

好吧 是我是在事务切入的地方切入错了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java 执行sessionFactory.getCurrentSession()报错