加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JDBC Transaction Management Example---reference

发布时间:2020-12-14 06:20:21 所属栏目:Java 来源:网络整理
导读:In this post,we want to talk about JDBC Transactions and how we can manage the operations in a database. The most popular DBMS like MySQL and Oracle have by default the option autocommit enabled,it means immediately after any DML Operation

In this post,we want to talk about JDBC Transactions and how we can manage the operations in a database.

The most popular DBMS like MySQL and Oracle have by default the option autocommit enabled,it means immediately after any DML Operation saves the changes and makes them visible to all users. To use transactions must set the databse parameter?autocommit?to false.

The management of the database using transaction allows us to maintain consistency in the data,according to his ‘ACID’ property.

Transaction Properties

What we want with Transactions? To Maintain this four properties:

  • Atomicity,it’s simple either all operations in database occur,or nothing occurs.
  • Consistency,ensures that the database is in a valid state before and after the transaction.
  • Isolation,any transaction is independent of another,and your result doesn’t depends of any other.
  • Durability,the result of commit a transaction must persist in a non-volatile memory even if occurs a crash or power loss.

Tools

For this example we use:

  1. JDK 1.7.0_67 (rt.jar includes java.sql package)
  2. Mysql-connector-java 5.1.34
  3. Eclipse Luna
  4. MySQL Community Server 5.6.22

1. Example:

DBConnection.java:

01?
02
03?
04?
05?
06
07
08
09
10
11??
12
13??
14??
15??
16
17???
18
19?
20
21

We use?DBConnection?only to get the connection,any other operation is handled in the main class.

DBTransaction.java:

01?
02
03?
04?
05?
06
07
08
09
10
11??
12
13??
14
15
16
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?
46
47
48?
49?
50
51
52
53
54?
55
56
57
58
59

The?connection.commit()?applies all the changes before him. The key is to disable the?autocommit?and to group the sentences to to manage them in a transaction with a final?commit.

We try to execute the transaction and this was the?result.

1
2
3

Here we should note that if one of the operations does not run correctly,all entries aren’t made and the database remains unchanged.

DBSavePoint.java:

01?
02
03?
04?
05?
06?
07
08
09
10
11
12??
13
14??
15
16????
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
46
47
48
49
50
51
52?
53?
54
55
56
57
58
59?
60
61
62
63
64
65

The method?setSavepoint?of class?Connection?allows to create a checkpoint internally in the transaction,and if a error occurs we can back to the savepoint with all of changes made before.

2. Summary

Here we tried to understand how to manage the JDBC Operations through transactions and how to make check points by means ofSavePoint?class.

http://examples.javacodegeeks.com/core-java/sql/jdbc-transaction-management-example/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读