解決 Connection is not transactional

来源:互联网 发布:网络推广专员 编辑:程序博客网 时间:2024/05/02 04:40

在项目开发中遇到这个异常Exception in thread "main" java.lang.IllegalStateException: Connection is not transactional,仔细的百度了一下,发现大多数的任都说是在查询之前没有connect数据库。但是我认真地分析了一下原因,发现问题的根源在于我的beans.xml没有将Connect加进事务管理。


解决方法,在beans.xml加进事务管理。


<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd"
       >
       
    <context:component-scan base-package="com.haiminhuang.test"/>
    <context:property-placeholder location="classpath:sys.properties"/>
    <context:annotation-config/>
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.user}" />
         <property name="password" value="${jdbc.password}" />
         <property name="maxActive" value="30" />
         <property name="initialSize" value="5" />
         <property name="maxWait" value="300" />
     </bean>
  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

      <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
     

</beans>


需要注意的是以下的这一句:

 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>,若不加proxy-target-class="true",很有可能会报

Can not set field to $Proxy异常。


0 0