KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > recovery > Raidb1RecoveryTransactionIdScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.recovery;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import org.objectweb.cjdbc.controller.core.Controller;
32 import org.objectweb.cjdbc.controller.recoverylog.RecoveryLog;
33 import org.objectweb.cjdbc.scenario.templates.Raidb1RecoveryTemplate;
34 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
35
36 /**
37  * This class defines a RecoveryTransactionIdScenario This was proposed by: Jeff
38  * Kolesky <jeff@edusoft.com> Start the controller. Begin issuing queries: A
39  * begin B insert into table_one ... C commit Restart the controller Issue
40  * queries: D begin E insert into table_two ... F rollback
41  *
42  * @author <a HREF="mailto:Nicolas Modrzyk@inria.fr">Nicolas Modrzyk</a>
43  * @version 1.0
44  */

45 public class Raidb1RecoveryTransactionIdScenario extends Raidb1RecoveryTemplate
46 {
47   /**
48    * Test transaction id of the recovery log is properly initialized from last
49    * index.
50    *
51    * @throws Exception if fails
52    */

53   public void testRecoveryLogTransactionID() throws Exception JavaDoc
54   {
55     // Get reference on the recovery log
56
RecoveryLog log = mainVdb.getRequestManager().getRecoveryLog();
57
58     // Get the first transaction id
59
long idZero = log.getLastTransactionId();
60
61     // Execute a transaction with INSERT that will update the recovery log
62
Connection JavaDoc con = getCJDBCConnection();
63     con.setAutoCommit(false);
64     Statement JavaDoc statement = con.createStatement();
65     statement.executeUpdate("INSERT INTO PRODUCT VALUES(50,'myproduct',5.5)");
66     con.commit();
67     statement.close();
68
69     // ReStart the controller
70
cm.stop("25322");
71     controller = (Controller) cm.start("25322").getProcess();
72     cm.loadVirtualDatabases(controller, "myDB", "hsqldb-raidb1-recovery.xml");
73     mainVdb = controller.getVirtualDatabase("myDB");
74     mainVdb.enableAllBackends();
75     // Get the last transaction id from the recovery log
76
log = mainVdb.getRequestManager().getRecoveryLog();
77     long firstId = log.getLastTransactionId();
78
79     // Execute a transaction and roll it back
80
con = getCJDBCConnection();
81     con.setAutoCommit(false);
82     Statement JavaDoc statement2 = con.createStatement();
83     statement2.executeUpdate("INSERT INTO DOCUMENT VALUES(50,32,5000.60)");
84     con.rollback();
85     statement2.close();
86
87     Statement JavaDoc statement3 = con.createStatement();
88     con.setAutoCommit(true);
89     ResultSet JavaDoc rs = statement3
90         .executeQuery("Select name from product where id=50");
91     assertNotNull("Result set should not be null after restarting controller",
92         rs);
93     String JavaDoc myproduct = null;
94     if (rs.next())
95       myproduct = rs.getString("name");
96
97     if (myproduct == null || myproduct.equalsIgnoreCase("myproduct") == false)
98       fail("Could not get rigth value for product");
99     long lastId = log.getLastTransactionId();
100
101     System.out.println("firstId:" + firstId + ":lastId:" + lastId + "idZero:"
102         + idZero);
103
104     Connection JavaDoc hsqlcon = getHypersonicConnection(9003);
105     ScenarioUtility.displaySingleQueryResult("Select * from RECOVERY", hsqlcon);
106     assertTrue("RecoveryLog firstId should be greater than idZero.",
107         firstId > idZero);
108     assertTrue("RecoveryLog lastId is not properly set with firstId.[" + lastId
109         + "==" + firstId + "]", lastId > firstId + 2);
110
111     statement3.close();
112     con.close();
113   }
114 }
Popular Tags