<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.zf"  />     

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">        <!-- 指定连接数据库的驱动 -->        <property name="driverClass" value="com.mysql.jdbc.Driver"/>        <!-- 指定连接数据库的URL -->        <property name="jdbcUrl" value="jdbc:mysql://192.168.1.140:3306/zftest"/>        <!-- 指定连接数据库的用户名 -->        <property name="user" value="root"/>        <!-- 指定连接数据库的密码 -->        <property name="password" value="root"/>        <!-- 指定连接数据库连接池的最大连接数 -->        <property name="maxPoolSize" value="20"/>        <!-- 指定连接数据库连接池的最小连接数 -->        <property name="minPoolSize" value="1"/>        <!-- 指定连接数据库连接池的初始化连接数 -->        <property name="initialPoolSize" value="1"/>        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->        <property name="maxIdleTime" value="20"/>     </bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property> 
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>  
<prop key="hibernate.show_sql">true</prop>  
</props>  
</property>
<property name="configLocations">   
<array>
<value>classpath:hibernate.cfg.xml</value>
</array>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
   
<aop:config>
<aop:pointcut expression="execution(* com.zf.service.*.*(..))" id="servicePoint"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="servicePoint" />  
</aop:config>  
  
<bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
   
</beans>
上面这种方式配置事务 , 支持事务 ,事务是有效的 。
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.zf"  />     

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://192.168.1.140:3306/zftest  </prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">root</prop>  
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>  
<prop key="hibernate.show_sql">true</prop>  
</props>  
</property>
<property name="configLocations">   
<array>
<value>classpath:hibernate.cfg.xml</value>
</array>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
   
<aop:config>
<aop:pointcut expression="execution(* com.zf.service.*.*(..))" id="servicePoint"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="servicePoint" />  
</aop:config>  
  
<bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
   
</beans>使用上面这种方式就不支持事务了。  难道非要使用数据源才能够配置事务吗? 不使用数据源该如何配置好事务?

解决方案 »

  1.   

    第二篇代码更正
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <context:component-scan base-package="com.zf"  />     
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                    <prop key="hibernate.connection.url">jdbc:mysql://192.168.1.140:3306/zftest  </prop>
                    <prop key="hibernate.connection.username">root</prop>
                    <prop key="hibernate.connection.password">root</prop>  
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>  
                    <prop key="hibernate.show_sql">true</prop>  
                </props>  
            </property>
            <property name="configLocations">   
                <array>
                    <value>classpath:hibernate.cfg.xml</value>
                </array>
            </property>
        </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            </tx:attributes>
        </tx:advice>
           
        <aop:config>
            <aop:pointcut expression="execution(* com.zf.service.*.*(..))" id="servicePoint"/>
            <aop:advisor advice-ref="transactionAdvice" pointcut-ref="servicePoint" />  
        </aop:config>  
          
        <bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
       
    </beans>