1这是spring的事物配置内容,如何在代码中进行手动回滚,如同使用jdbc的时候,进行的事物回滚
<?xml version="1.0" encoding="UTF-8"?>
2 3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:aop="http://www.springframework.org/schema/aop"
8 xmlns:tx="http://www.springframework.org/schema/tx"
9 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
10 xmlns:cache="http://www.springframework.org/schema/cache"
11 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
15 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
16 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 org.hibernate.dialect.MySQLDialect
46 true
47 thread
48
49
50
51
52
53 po
54
55
56
57
58
59 <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
60
61
62
63
64
65 tx:attributes
66
67
68 /tx:attributes
69 /tx:advice
70
71
72 <!-- -->
73
74
75 /aop:config
76
77
这是jdbc的事物回滚的代码
public static void save(String name,int age)
{
Connection conn=null;
PreparedStatement ps=null;
try
{
conn=DBUtils.getConnection();
//把自动提交事务改为false
conn.setAutoCommit(false);
ps=conn.prepareStatement("insert into student(sname,age) value(?,?)");
ps.setString(1,name);
ps.setInt(2, age);
ps.executeUpdate();
//提交事务
conn.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.close(null, ps, conn);
}
}
public static void add(String name,int age)
{
Connection conn=null;
PreparedStatement ps=null;
try
{
conn=JDBCUtils.getConnection();
ps=conn.prepareStatement("insert into student(sname,age) value(?,?)");
for(int i=101;i<201;i++)
{
ps.setString(1,name+i);
ps.setInt(2, age);
ps.addBatch();
}
ps.executeBatch();
}catch(Exception e){
e.printStackTrace();
}finally{
DBUtils.close(null, ps, conn);
}
}
使用上面的spring配置,如何才能在代码中如同jdbc一样进行try{}catch{}的手动的事物回滚。请大神知道一下,最好贴一下代码。
http://blog.csdn.net/rocklee/article/details/51699116
Hibernate的session实例就有啊。比如beginTransction(),commit(),等等啊。和jdbc一样的啊