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 package fr.paris.lutece.util.sql;
35
36 import fr.paris.lutece.test.LuteceTestCase;
37
38 import java.sql.SQLException;
39
40
41
42
43 public class TransactionTest extends LuteceTestCase
44 {
45 private static final String SQL_DROP_TABLE = "DROP TABLE IF EXISTS test_transaction";
46 private static final String SQL_CREATE_TABLE = "CREATE TABLE test_transaction ( id integer )";
47 private static final String SQL_INSERT = "INSERT INTO test_transaction VALUES ( ? )";
48
49 public void testCommit( )
50 {
51 System.out.println( "commit" );
52
53 Transaction transaction = new Transaction( );
54
55 try
56 {
57 transaction.prepareStatement( SQL_DROP_TABLE );
58 transaction.executeStatement( );
59 transaction.prepareStatement( SQL_CREATE_TABLE );
60 transaction.executeStatement( );
61
62 for ( int i = 0; i < 3; i++ )
63 {
64 transaction.prepareStatement( SQL_INSERT );
65 transaction.getStatement( ).setInt( 1, i );
66 transaction.executeStatement( );
67 }
68
69 transaction.commit( );
70 }
71 catch( SQLException ex )
72 {
73 transaction.rollback( ex );
74 }
75
76 assertTrue( transaction.getStatus( ) == Transaction.COMMITTED );
77 }
78
79 public void testRollback( )
80 {
81 System.out.println( "rollback" );
82
83 Transaction transaction = new Transaction( );
84
85 try
86 {
87 for ( int i = 3; i < 6; i++ )
88 {
89 transaction.prepareStatement( SQL_INSERT );
90 transaction.getStatement( ).setInt( 1, i );
91 transaction.executeStatement( );
92 }
93
94 transaction.rollback( );
95 }
96 catch( SQLException ex )
97 {
98 transaction.rollback( ex );
99 }
100
101 assertTrue( transaction.getStatus( ) == Transaction.ROLLEDBACK );
102 }
103 }