我想通过sping注释方法来实现事务,结果发现事务没生效?谁帮我看看那里出问题啦。谢谢了。我测试时是执行一个本地html文件,跨域访问dwr服务器 ,然后在TestDwr中实现事务。applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-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/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- <context:annotation-config /> --><context:component-scan base-package="com.test.dwr">
    <context:include-filter type="regex" 
        expression="com\.test\.dwr\..*"/>
</context:component-scan><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName">   
        <value>oracle.jdbc.driver.OracleDriver</value>   
    </property>   
    <property name="url">              
        <value>jdbc:oracle:thin:@192.168.1.111:1521:ORA11</value>   
     </property>   
    <property name="username">   
        <value>scott</value>   
    </property>   
    <property name="password">   
        <value>tiger</value>   
    </property>   
</bean>    <bean id ="JdbcTemplate"
   class ="org.springframework.jdbc.core.JdbcTemplate"> 
   <property name ="dataSource"> 
      <ref bean ="dataSource"  /> 
   </property> 
</bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean><tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>   <!--
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">   
       <property name="jndiName">   
       <value>java:/comp/env/jdbc/DataSource</value>
      </property>   
    </bean>    
-->
</beans>
TestDwr.javapackage com.test.dwr;import java.sql.*;
import java.util.*;import javax.sql.DataSource;
import javax.naming.InitialContext;import org.apache.log4j.Logger;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;import com.test.dao.BaseDao;
import com.test.util.CommonUtil;@Component("BaseDao")
@Transactional
public class TestDwr implements BaseDao{ private DataSourceTransactionManager transactionManager;
private DefaultTransactionDefinition def;
private JdbcTemplate jdbcTemplate;   
    public JdbcTemplate getJdbcTemplate() {   
        return jdbcTemplate;   
    }   

    @Transactional
public String getval(){
String ret = "";
try {
//         ClassPathResource res = new ClassPathResource("applicationContext.xml");
//         XmlBeanFactory factory = new XmlBeanFactory(res);
//         JdbcTemplate jdbcTemplate = (JdbcTemplate) factory.getBean("JdbcTemplate");
        ApplicationContext ctx = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate =(JdbcTemplate)ctx.getBean("boss");
              System.out.println(jdbcTemplate);         String sql1 = "insert into dept(deptno,dname,loc) values (13,'eagle11','bj11')";
String sql2 = "inser into dept(deptno,dname,loc) values (15,'eagle2','bj22')";
jdbcTemplate.update(sql1);
jdbcTemplate.update(sql2);
         //setDataSource(ds);
//tstatus = transactionManager.getTransaction(def);//  List deplist =
//  jdbcTemplate.queryForList("select * from dept ");
//  Iterator itor = deplist.iterator();
//  while(itor.hasNext()){
//  Map depmap = (Map)itor.next();
//  ret += depmap.get("dname") + "," + depmap.get("loc");
//  } // insert test
// String sql1 = "insert into dept(deptno,dname,loc) values (13,'eagle11','bj11')";
// String sql2 = "insert into dept(deptno,dname,loc) values (15,'eagle2','bj22')";
// jdbcTemplate.update(sql1);
// jdbcTemplate.update(sql2);
//
// Logger log = Logger.getLogger(TestDwr.class);
//         log.info("info..........."); } catch (Exception e) {
e.printStackTrace();
}
return ret; } public static void main(String[] args) {
// TODO Auto-generated method stub }}

解决方案 »

  1.   

    缺事务处理的代理吧。。类次:org.springframework.transaction.interceptor.TransactionProxyFactoryBean
    或配置事务策
      

  2.   

    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> 不是开启了处理事务的代理么。在<tx:annotation-driven/>元素上的"proxy-target-class" 属性 控制了有什么类型的事务性代理会为使用@Transactional 来注解的类创建代理。 如果"proxy-target-class" 属性被设为"true",那么基于类的代理就会被创建。 如果"proxy-target-class" 属性被设为"false" 或者没设,那么会创建基于接口的标准JDK代理
      

  3.   

    sorry..看少了...那就无能为力..等待高手。
      

  4.   

    ----applicationContext.xml 
     <content:property-placeholder location="classpath:dbcpconfig.properties"/>
    <content:component-scan base-package="cn.itcast"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    <property name="initialSize" value="${initialSize}"/>
    <property name="maxActive" value="${maxActive}"/>
    <property name="maxIdle" value="${maxIdle}"/>
    <property name="minIdle" value="${minIdle}"/>
    <property name="maxWait" value="${maxWait}"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>----service@Service("personService")
    @Transactional
    public class PersonServiceBean implements PersonService {
    private SimpleJdbcTemplate jdbcTemplate;

    @Resource
    public void setDataSource(DataSource dataSource){
    jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }
    public void delete(Integer id) {
    jdbcTemplate.update("delete from person where id = ?", id);
    } public Person getPersonById(Integer personid) {
    return jdbcTemplate.queryForObject("select id, name, password from person where id = ?", ParameterizedBeanPropertyRowMapper.newInstance(Person.class), 
    personid);
    }楼主自己参考下吧
      

  5.   


    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
          org.hibernate.dialect.OracleDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingLocations">
    <value>classpath:/com/revic/lms/*/po/*.hbm.xml</value>
    </property>
    <property name="lobHandler">
    <ref bean="lobHandler" />
    </property>
    </bean>
    <bean id="nativeJdbcExtractor" lazy-init="true"  class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"/> <bean id="lobHandler" lazy-init="true"   class="org.springframework.jdbc.support.lob.OracleLobHandler">
            <property name="nativeJdbcExtractor">
                <ref bean="nativeJdbcExtractor"/>
            </property>
    </bean> 
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean> <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="modify*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="*" read-only="true" />
    </tx:attributes>
    </tx:advice> <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
    <aop:pointcut id="allManagerMethod"
    expression="execution(* com.revic.lms.*.*(..))" />
    <aop:advisor pointcut-ref="allManagerMethod"
    advice-ref="txAdvice" />
    </aop:config>
      

  6.   

    谢谢各位,看的我眼花缭乱的,还是没人告诉我。我哪里配错了。。
    java四脚猫:我看我的配置不比你的少啊
      

  7.   

    我参考四角猫,把jdbctemplate 在applicationcontext定义删除, private SimpleJdbcTemplate jdbcTemplate;
    @Autowired
    public void setDataSource(DataSource dataSource){ 
    jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
    } 采用自动注入方式,结果也没自动注入。怎么我定义的元数据一个都没生效啊。
      

  8.   

    @Resource(name = "dataSource")
    public void setDataSource(DataSource dataSource){ 
    jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
    System.out.println("jdbcTemplate111:-----" +jdbcTemplate);
    } 应用服务器启动时:显示 jdbcTemplate111:-----: org.springframework.jdbc.core.simple.SimpleJdbcTemplate@11a663我在getval中调用jdbcTemplate时怎么显示 null 呢
      

  9.   

    不知道你getVal方法修改成啥样子了?
    private JdbcTemplete jdbcTemplete; 
    @Resource(name = "dataSource") 
    public void setDataSource(DataSource dataSource){ 
    jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
    System.out.println("jdbcTemplate111:-----" +jdbcTemplate); 
    }public void getVal(){
       // 直接使用红色部分的这个就ok啦,
    jdbcTemplete.queryForList("".....);
    }
      

  10.   

    对了,我使用的是SimpleJdbcTemplete,你注意换一下哈
      

  11.   

    我郁闷极了,服务器启动时日志明明显示 jdbctemplate已经装配成功了。 但我在方法中调用时,却显示jdbctemplate为null。。算了,先不说这个问题了,这个我继续写在xml中好了。我关注的主要是为何我配置了@transactional,却没有成功控制住事务,我插入2sql ,一个正确一个错误。应该一条记录都插不进去啊,但我却看到正确那条sql进库了。
      

  12.   

    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    红色部门删除掉看看.
    既然使用注解开启事务,我没用过红色部分的,你试下
      

  13.   

    <context:component-scan base-package="com.test.dwr"> 
        <context:include-filter type="regex" 
            expression="com\.test\.dwr\..*"/> 

    </context:component-scan> 
    这个也删除掉看看
      

  14.   

    貌似JDBCTemplate是autocommited ,事务不好实现吧。   我在网上面看过一个jdbctmplate使用proxy的实现例子,好像很麻烦,谁能给我提供一个jdbctmplate使用@transactional 实现事务的例子呢。。