我想通过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 }}
<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 }}
或配置事务策
<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);
}楼主自己参考下吧
<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>
java四脚猫:我看我的配置不比你的少啊
@Autowired
public void setDataSource(DataSource dataSource){
jdbcTemplate = new SimpleJdbcTemplate(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 呢
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("".....);
}
红色部门删除掉看看.
既然使用注解开启事务,我没用过红色部分的,你试下
<context:include-filter type="regex"
expression="com\.test\.dwr\..*"/>
</context:component-scan>
这个也删除掉看看