读书人

spirng宣言式事务官方文档例子 不回

发布时间: 2013-02-19 11:11:40 作者: rapoo

spirng声明式事务,官方文档例子 不回滚
本帖最后由 wuwei35531 于 2013-02-05 21:31:45 编辑 各位大牛,最近在学习spring 声明式事务配置,完成后总是无法回滚,于是看了spring transaction官方文档,并写了官方的例子,http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html
发现仍然不能回滚,找不到原因,所以只能上论坛请教各位大神

下面是代码
包x.y.service
-- Boot.java

package x.y.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Boot {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml", Boot.class);
FooService fooService = (FooService)ctx.getBean("fooService");
fooService.insertFoo(new Foo());
}
}
-- Foo.java

package x.y.service;

public class Foo {
private String fooName;
private String barName;
public Foo() {
super();
}
public Foo(String fooName, String barName) {
super();
this.fooName = fooName;
this.barName = barName;
}
public String getFooName() {
return fooName;
}
public void setFooName(String fooName) {
this.fooName = fooName;
}
public String getBarName() {
return barName;
}
public void setBarName(String barName) {
this.barName = barName;
}
}
-- FooService.java

package x.y.service;

public interface FooService {
public Foo getFoo(String fooName);
public Foo getFoo(String fooName, String barName);
public void insertFoo(Foo foo);
public void updateFoo(Foo foo);
}
-- DefaultFooService.java

package x.y.service;


public class DefaultFooService implements FooService {

@Override
public Foo getFoo(String fooName) {
throw new UnsupportedOperationException();
}

@Override
public Foo getFoo(String fooName, String barName) {
throw new UnsupportedOperationException();
}

@Override
public void insertFoo(Foo foo) {
throw new UnsupportedOperationException();
}

@Override
public void updateFoo(Foo foo) {
throw new UnsupportedOperationException();
}
}

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="fooService" class="x.y.service.DefaultFooService">
</bean>



<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>

</beans>


报错
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Exception in thread "main" java.lang.UnsupportedOperationException
at x.y.service.DefaultFooService.insertFoo(DefaultFooService.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy0.insertFoo(Unknown Source)
at x.y.service.Boot.main(Boot.java:14)
警告暂时忽略,没有配置log4j
而官方的报错,会提醒发生回滚
<!-- the Spring container is starting up... -->
[AspectJInvocationContextExposingAdvisorAutoProxyCreator] - Creating implicit proxy
for bean 'fooService' with 0 common interceptors and 1 specific interceptors


<!-- the DefaultFooService is actually proxied -->
[JdkDynamicAopProxy] - Creating JDK dynamic proxy for [x.y.service.DefaultFooService]

<!-- ... the insertFoo(..) method is now being invoked on the proxy -->

[TransactionInterceptor] - Getting transaction for x.y.service.FooService.insertFoo
<!-- the transactional advice kicks in here... -->
[DataSourceTransactionManager] - Creating new transaction with name [x.y.service.FooService.insertFoo]
[DataSourceTransactionManager] - Acquired Connection
[org.apache.commons.dbcp.PoolableConnection@a53de4] for JDBC transaction

<!-- the insertFoo(..) method from DefaultFooService throws an exception... -->
[RuleBasedTransactionAttribute] - Applying rules to determine whether transaction should
rollback on java.lang.UnsupportedOperationException
[TransactionInterceptor] - Invoking rollback for transaction on x.y.service.FooService.insertFoo
due to throwable [java.lang.UnsupportedOperationException]

<!-- and the transaction is rolled back (by default, RuntimeException instances cause rollback) -->
[DataSourceTransactionManager] - Rolling back JDBC transaction on Connection
[org.apache.commons.dbcp.PoolableConnection@a53de4]
[DataSourceTransactionManager] - Releasing JDBC Connection after transaction
[DataSourceUtils] - Returning JDBC Connection to DataSource

Exception in thread "main" java.lang.UnsupportedOperationException
at x.y.service.DefaultFooService.insertFoo(DefaultFooService.java:14)
<!-- AOP infrastructure stack trace elements removed for clarity -->
at $Proxy0.insertFoo(Unknown Source)
at Boot.main(Boot.java:11)


小弟之前用spring+hibernate spring声明式事务,写了一个数据库操作,发现无法回滚,一直找不到原因,所以看了spring官方文档,并实现例子,发现仍然不行,这个问题困扰几天了,一直没解决,所以来论坛请教各位牛人,谢谢大家 spring hibernate transaction
[解决办法]
官方例子不一定完整,自己按那思想,做一个demo看是否有问题就行了。

读书人网 >J2EE开发

热点推荐