DAO中
@Repository
public class TestDao extends BaseHibernateDao {
public void test(){
String hql = " update ZTest set uname = ? where id = 5 ";
Query q = this.getSession(true).createQuery(hql);
q.setString(0, "x");
q.executeUpdate();
String hql2 = " update ZTest set unadfme = ? where id = 5";
Query q2 = this.getSession(true).createQuery(hql2);
q2.executeUpdate();
}
}
SERVICE中@Service
public class ServiceTest extends BaseService {
@Autowired
TestDao testDao; @Override
public void destroy() { } @Override
public void init() { }
@Transactional
public void testTransactional(){
testDao.test();
} }CONTROLLER中@Controller
public class PLetterController extends BaseController {
@Autowired
ServiceTest serviceTest;
@RequestMapping("/testT.do")
public void test(Writer writer) throws Exception{
serviceTest.testTransactional();
}
}SPRING 管理 HIBERNATE <?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: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-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-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:省略..........</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.XXXXXXX.domain</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
</beans>大侠救命!
@Repository
public class TestDao extends BaseHibernateDao {
public void test(){
String hql = " update ZTest set uname = ? where id = 5 ";
Query q = this.getSession(true).createQuery(hql);
q.setString(0, "x");
q.executeUpdate();
String hql2 = " update ZTest set unadfme = ? where id = 5";
Query q2 = this.getSession(true).createQuery(hql2);
q2.executeUpdate();
}
}
SERVICE中@Service
public class ServiceTest extends BaseService {
@Autowired
TestDao testDao; @Override
public void destroy() { } @Override
public void init() { }
@Transactional
public void testTransactional(){
testDao.test();
} }CONTROLLER中@Controller
public class PLetterController extends BaseController {
@Autowired
ServiceTest serviceTest;
@RequestMapping("/testT.do")
public void test(Writer writer) throws Exception{
serviceTest.testTransactional();
}
}SPRING 管理 HIBERNATE <?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: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-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-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:省略..........</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.XXXXXXX.domain</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
</beans>大侠救命!
解决方案 »
- 页面不跳转
- java 调用 oracle 11g 存储过程出错
- ssh整合问题 No getter method for property skuname of bean
- 各位大侠,struts2里用什么命令读取web.xml里的context-param里的配置参数呢
- 大家帮帮忙吧:警告: No configuration found for the specified action: 'login'
- 关于jsp页面中的text提交后,特殊符号内容的处理
- 为什么用request.getInputStream()
- Eclipse3.6的问题,希望能得到回答
- 上传文件enctype="multipart/form-data" 页面参数获得问题
- javax.servlet.ServletException: No ResultSet was produced
- 在接口方法前面的@WebMethod(exclude=true),这样设置是什么意思啊?
- jsp生成word,然后在jsp页面如何设置word的边距???
http://www.21java.com/html/19/t-1719.htmlhttp://justry28.javaeye.com/blog/264241
这里说的很有道理,楼主可以试一下,把service的实现类排除在外
好像你的代码里面没有Set注入哦
IOC都没有
怎么让AOP帮你管理事物?
注解@Autowired就已经注入了
http://www.javaeye.com/problems/2646,我没有测试过,这里说是spring2.5的一个bug
我一直用的都是声明式的,有时间我会测试一下。
不知道spring跟
实现.开发人员提交后.用勾子自动同步新的版本到外面的服务器的测试环境上有
关系没?
<bean id="adminServiceImpl" class="com.yudylaw.ebank.service.impl.AdminServiceImpl" parent="adminService">
<property name="adminDao">
<ref bean="adminDaoImpl"/>
</property>
</bean>
Dao你可以直接使用你自己那样的注解,无需在配置文件中注入了。
已经有HIBERNATE输出了2008-12-22 12:51:26 [ http-8080-2:9672 ] - [ INFO ] OSCache: New cache entry, cache stale or cache scope flushed for /p/testT.do_GET_
Hibernate:
update
MANAGOOD.Z_TEST
set
UNAME=?
where
ID=5
Hibernate:
update
MANAGOOD.Z_TEST
set
unadfme=?
where
ID=5
用的是Oracle数据库我在数据库里已经看到 字段被更新了,就是说当异常发生的时候,SPRING AOP没有拦截住ORZZZZZZZZZZZZZ
先不用@Service 注释,测试一下
用如下代码把你的service累排除在外,然后再xml中注入一下
<context:component-scan base-package="com.xxx.xxx">
<context:exclude-filter type="aspectj"
expression="com.xxx.xxx.ServiceTest" />
</context:component-scan>
<bean name="serviceTest" class="com.mycom.modules.demo.ServiceTest" />
测试一下事务是否完整!
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="serviceTest" class="com.xxx.xxx.service.ServiceTest">
<property name="letterDao" ref="letterDao" />
</bean>
<context:component-scan base-package="com.xxx.xx">
<context:exclude-filter type="aspectj" expression="com.xxx.xxx.ServiceTest" />
</context:component-scan> 之后报找不到letterDao
LetterDao LetterDao;
public void setLetterDao(LetterDao LetterDao) {
this.LetterDao = LetterDao;
}
真是郁闷
<property name="target">
<bean class="com.gainuo.elec.service.impl.ListeningClozeTitleServiceImpl">
<property name="dao">
<bean class="com.gainuo.elec.dao.hibernate.ListeningClozeTitleDAOImpl">
<property name="sessionFactory">
<ref local="mysessionFactory"/>
</property>
</bean>
</property>
</bean>
</property>
</bean><bean id="proxytemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!-- 事务代理模板 -->
<property name="transactionManager">
<ref local="myTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean name="serviceTest" class="com.mycom.modules.demo.ServiceTest" >
<property name="testDao" ref="testDao"></property>
</bean>
<bean id="testDao" class="xxx.yyy.TestDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
......
顺便问一下,楼主为什么要用编程方式事务,呵呵
发现个问题:
大小写改一下LetterDao LetterDao;
改成LetterDao letterDao;
否则set会无效
网址如下:
http://www.blogjava.net/wangbing/archive/2008/10/08/233203.html
http://www.javaeye.com/problems/2646
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> 这种方式是最简便的啊在需要控制事务的XxxService的某个具体方法上加上@Transactional注解就可以了AspectJ太强大,我现在的项目中暂时还不需要这么细粒度的AOP
别忘了
大小写改一下LetterDao LetterDao;
改成LetterDao letterDao;
只需把serviceTest拿到xml配置,其他的还是用annotation注入即可
呵呵,希望你能逐一的看一下我的回复,祝你好运
被context:component-scan管理进来的不同package下的同样名字的bean,
就会出doesn't recognize same beans when included multiple times on classpath错误。同时强烈建议楼主采用声明式事务,配置一次即可!免去了烦琐的@Transactional
如何声明式事务?我就是想一次搞定,不喜欢增量开发,喜欢注解,不喜欢XML的一个懒人。。@_@我是参考的1. 声明式事务管理 - Spring Framework reference 2.0.5 参考手册中文版
URL : http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09s05.html
bean和事务是脱离的,二者没有关系,例子给你看一下,你可以改写一下
<context:component-scan base-package="*" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
......
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
......
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<!--这里配置规则,满足以下规则的将触发事务,第一个*表示所有返回类型,第二个表示service包下的所有class,第三个表示所有方法-->
<aop:pointcut id="baseServiceMethods"
expression="execution(* xxx.yyy.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--对于方法的细粒度事务配置,符合条件的方法冠以相应的事务-->
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
为何这样配置后,我在public void test(){
String hql = " update ZTest set uname = ? where id = 5 ";
Query q = this.getSession(true).createQuery(hql);
q.setString(0, "1");
q.executeUpdate();
throw new IllegalStateException();
}后,数据字段还是更新了。。
我的dao的service都不是基于接口的,能用吗?
PS:
我的目标是 ----> ^_^
spring版本:2.5,用声明式事务
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
......
<context:component-scan base-package="com.huaxia.oaapp">
<context:include-filter type="aspectj" expression="com.huaxia.oaapp.service..*"/>
<context:include-filter type="aspectj" expression="com.huaxia.oaapp.entity..*"/>
<context:exclude-filter type="aspectj" expression="com.huaxia.oaapp.action..*"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
......
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
<!-- <prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop> -->
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.huaxia.oaapp.entity.Document</value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value>com.huaxia.oaapp.entity</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="baseServiceMethods"
expression="execution(* com.huaxia.oaapp.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config>
<!-- <aop:aspectj-autoproxy /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
Document类如下:@Entity
@Table(name="Documents")
public class Document {
@Id
// @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String sn;
get/set......documentService代码如下:
package com.huaxia.oaapp.service;
......
@Component("documentService")
public class DocumentServiceImpl /*implements DocumentService */{
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/* (non-Javadoc)
* @see com.huaxia.oaapp.service.DocumentService#saveOrUpdateDocument(com.huaxia.oaapp.entity.Document)
*/
public void saveOrUpdateDocument(Document document11){
Document document = new Document();
document.setId(10l);
document.setSn("10");
this.sessionFactory.getCurrentSession().flush();
Document document1 = new Document();
document1.setId(11l);
document1.setSn("11");
//document1.setUser(user);
this.sessionFactory.getCurrentSession().saveOrUpdate(document);
this.sessionFactory.getCurrentSession().save(document1);
}
/* (non-Javadoc)
* @see com.huaxia.oaapp.service.DocumentService#findDocumentById(java.lang.Long)
*/
public Document findDocumentById(Long id) {
return (Document) this.sessionFactory.getCurrentSession().get(Document.class,
id);
}
......
我的junit测试类如下:
public class UserServiceTest {
private DocumentServiceImpl documentService;
@Before
public void setUp() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
this.documentService = (DocumentServiceImpl) ctx.getBean("documentService");
}
@Test
public void testSaveOrUpdateUser() {
this.documentService.saveOrUpdateDocument(null);
}
}
注意:我注释掉了/*implements DocumentService */部分,启动cglib代理
2008-12-22 22:37:34,569 DEBUG [org.springframework.aop.framework.Cglib2AopProxy] - Creating CGLIB2 proxy: target source is SingletonTargetSource for target object [com.huaxia.oaapp.service.DocumentServiceImpl@eca36e]
......
当saveOrUpdateDocument方法执行时,事务启动
......
2008-12-22 22:40:23,819 DEBUG [org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method [select*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly]
2008-12-22 22:40:23,819 DEBUG [org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method [find*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly]
2008-12-22 22:40:23,819 DEBUG [org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method [save*] with attribute [PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ]
......
当调用失败时(我这里用的是sqlserver,楼主可以看控制台的log4j,这里记载着详细的信息,包括事务和cglib代理是否成功):
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_Documents'. Cannot insert duplicate key in object 'dbo.Documents'.
......
2008-12-22 22:40:24,225 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Initiating transaction rollback after commit exception
成功回滚
贴了好一大堆,哈哈,我这里测试已经通过,楼主可以参考一下。
public void test(){
String param = "sdfsdfsdf";
String sql = " update z_test set uname = '" + param + "' where id = 5 ";
this.getJdbcTemplate().queryForList(sql);
String sql2 = " update z_test set unamfe = 'sdfsdfsd' where id = 5 ";
this.getJdbcTemplate().queryForList(sql2);
}我在service中调了这个方法那个sql2中 unamfe在数据库是没有的字段,执行的时候会抛异常,照理说,事务会回滚的,但是我在数据库还是看到了 id为5的 uname字段改成了 sdfsdfsdf
我的配置文件<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="baseServiceMethods"
expression="execution(* com.xx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config>
<!-- <aop:aspectj-autoproxy /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
PS:我的SPRINGMVC 放在 dispatch-servlet.xmlhibernate放在 spring-hibernate.xmlspring security 放在 spring-security.xml这样配置是不是有问题?
JdbcTemplate不使用的HibernateTransactionManager呀,用的是DataSourceTransactionManager有点乱呀,呵呵,你根本没加入事务!
我是没折了,把各种Manager排列组合试一下,真是郁闷,搞了4天了
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
......
</tx:attributes>
这里要包含你的那个方法,比如你的serviceTest中的方法是
public void testTransactional(){
testDao.test();
}
那么需要把这个方法加入进来
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="testTransactional" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
......
</tx:attributes>
这个是通配符,就是方法名字满足这个通配符的才被加入事务!你先屡一下思路吧
因为我基础不好,错加了2个1.org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor2.org.springframework.orm.hibernate3.support.OpenSessionInViewFilter去掉其中一个就OK了本人比较喜欢基于非接口的 cglib动态代理事务编程式事务估计很少人用谢谢 Landor2004的热心回答,谢谢CSDN的网友,结贴!
PS:
我的目标是 ----> ^_^
PS:
我的目标是 ----> ^_^
PS:
我的目标是 ----> ^_^
http://blog.csdn.net/masuwen/article/details/5445349