-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPostgreDemoACID.java
More file actions
54 lines (45 loc) · 1.5 KB
/
PostgreDemoACID.java
File metadata and controls
54 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
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
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author cscharff Sample of JDBC for PostgreSQL
* ACID is implemented
*/
public class PostgreDemoACID {
public static void main(String args[]) throws SQLException, IOException,
ClassNotFoundException {
// Load the PostgreSQL driver
// Connect to the default database with credentials
// You will have to change your credentials
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "postgres");
// For atomicity
conn.setAutoCommit(false);
// For isolation
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
Statement stmt1 = null;
try {
// Create statement object
stmt1 = conn.createStatement();
// Maybe a table student exists, maybe not
// create table student(id integer, name varchar(10), primary key(id))
// Either the 2 following inserts are executed, or none of them are. This is
// atomicity
stmt1.executeUpdate("insert into student values (7, 'stud1')");
stmt1.executeUpdate("insert into student values (2, 'stud2')");
} catch (SQLException e) {
System.out.println("An exception was thrown");
e.printStackTrace();
// For atomicity
conn.rollback();
stmt1.close();
conn.close();
return;
}
conn.commit();
stmt1.close();
conn.close();
}
}