KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > hibernate > ImolaHibernate


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.teams.hibernate;
21
22 import java.io.*;
23
24 import org.polepos.circuits.imola.*;
25 import org.polepos.framework.*;
26 import org.polepos.teams.hibernate.data.*;
27
28 import net.sf.hibernate.*;
29
30 public class ImolaHibernate extends HibernateDriver implements ImolaDriver {
31     private final String JavaDoc FROM = "from org.polepos.teams.hibernate.data.HibernateIndexedPilot";
32     private Serializable[] ids;
33     private int step;
34     
35     public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException{
36         ids=new Serializable[setup.getSelectCount()];
37         super.takeSeatIn(car, setup);
38     }
39
40     public void store() {
41         try{
42             Transaction tx = db().beginTransaction();
43             for ( int i = 1; i <= setup().getObjectCount(); i++ ){
44                 storePilot(tx, i);
45             }
46             tx.commit();
47         }
48         catch ( HibernateException hex ){
49             hex.printStackTrace();
50         }
51     }
52
53     public void retrieve() {
54         for(Serializable id : ids) {
55             try {
56                 HibernateIndexedPilot pilot=(HibernateIndexedPilot) db().load(HibernateIndexedPilot.class,id);
57                 addToCheckSum(pilot.getPoints());
58                 if(pilot==null) {
59                     System.err.println("Got no pilot by ID.");
60                 }
61             } catch (HibernateException e) {
62                 e.printStackTrace();
63             }
64         }
65     }
66
67     private void storePilot(Transaction trans, int idx) throws HibernateException {
68         Serializable id=db().save( new HibernateIndexedPilot( "Pilot_" + idx, "Jonny_" + idx, idx , idx ) );
69         if ( isCommitPoint(idx) ){
70             trans.commit();
71             Log.logger.fine( "commit while writing at " + idx+1 ); //NOI18N
72
}
73         if(idx <= setup().getSelectCount()) {
74             ids[idx - 1] = id;
75         }
76     }
77
78     private boolean isCommitPoint(int idx) {
79         int commitInterval = setup().getCommitInterval();
80         return commitInterval> 0 && idx%commitInterval==0 && idx < setup().getObjectCount();
81     }
82 }
83
Popular Tags