Ik ben aan het proberen om transactions werkend te krijgen in Java. Dit staat hier uitgelegd. Nu is mijn probleem dat het niet wil werken. Ik doe expres het 2e statement fout, zodat 'ie in principe een rollback zou moeten uitvoeren. Helaas voert hij de eerste statement gewoon uit als de 2e fout is, er word geen rollback uitgevoerd.
Iemand een idee wat ik fout doe?
Ik draai PostgreSQL 8.0.0 beta 5 met de bijbehorende JDBC-connector, die transactions gewoon ondersteunt.
Java:
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
| public static String testTrans() throws Exception { Class.forName("org.postgresql.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:"+u+""+d+"?user="+l+"&password="+p+""); con.setAutoCommit(false); try { String query1 = "UPDATE table1 SET blaat = 'blaat' WHERE id = 1"; PreparedStatement st1 = con.prepareStatement(query1); int result1 = st1.executeUpdate(); } catch(SQLException ex) { con.rollback(); } try { // expres een niet bestaande id: String query2 = "UPDATE table1 SET blaat = 'blaat' WHERE id = 765"; PreparedStatement st2 = con.prepareStatement(query2); int result2 = st2.executeUpdate(); } catch(SQLException ex) { con.rollback(); } con.commit(); con.setAutoCommit(true); return "gelukt"; } |
Iemand een idee wat ik fout doe?
Ik draai PostgreSQL 8.0.0 beta 5 met de bijbehorende JDBC-connector, die transactions gewoon ondersteunt.
They made me do it.