KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > idgen > AmberTableGenerator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.idgen;
31
32 import com.caucho.amber.manager.AmberConnection;
33 import com.caucho.amber.manager.AmberPersistenceUnit;
34 import com.caucho.amber.type.GeneratorTableType;
35 import com.caucho.util.L10N;
36 import com.caucho.util.Log;
37
38 import javax.sql.DataSource JavaDoc;
39 import java.sql.Connection JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.sql.Statement JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Generator table.
48  */

49 public class AmberTableGenerator extends IdGenerator {
50   private static final L10N L = new L10N(AmberTableGenerator.class);
51   private static final Logger JavaDoc log = Log.open(AmberTableGenerator.class);
52   
53   private AmberPersistenceUnit _manager;
54   private GeneratorTableType _table;
55   private String JavaDoc _name;
56
57   private String JavaDoc _selectSQL;
58   private String JavaDoc _updateSQL;
59
60   private boolean _isInit;
61   
62   /**
63    * Creates the table generator.
64    */

65   public AmberTableGenerator(AmberPersistenceUnit manager,
66                  GeneratorTableType table,
67                  String JavaDoc name)
68   {
69     _manager = manager;
70     _table = table;
71     _name = name;
72   }
73
74   /**
75    * Allocates the next group of ids.
76    */

77   public long allocateGroup(AmberConnection aConn)
78     throws SQLException JavaDoc
79   {
80     int groupSize = getGroupSize();
81     
82     int retry = 5;
83
84     // XXX: should use non-XA
85
Connection conn = aConn.getConnection();
86     PreparedStatement JavaDoc selectStmt = conn.prepareStatement(_selectSQL);
87     PreparedStatement JavaDoc updateStmt = conn.prepareStatement(_updateSQL);
88
89     selectStmt.setString(1, _name);
90     updateStmt.setString(2, _name);
91
92     while (retry-- > 0) {
93       ResultSet JavaDoc rs = selectStmt.executeQuery();
94       if (rs.next()) {
95     long value = rs.getLong(1);
96     rs.close();
97
98     updateStmt.setLong(1, value + groupSize);
99     updateStmt.setLong(3, value);
100
101     if (updateStmt.executeUpdate() == 1)
102       return value;
103       }
104       rs.close();
105     }
106
107     throw new SQLException JavaDoc(L.l("Can't allocate id from table '{0}'",
108                    _table.getTable().getName()));
109   }
110
111   /**
112    * Initialize the table.
113    */

114   public void init(AmberPersistenceUnit amberPersistenceUnit)
115     throws SQLException JavaDoc
116   {
117     if (_isInit)
118       return;
119     _isInit = true;
120
121     _selectSQL = ("select " + _table.getValueColumn() +
122           " from " + _table.getTable().getName() +
123           " where " + _table.getKeyColumn() + "=?");
124
125     _updateSQL = ("update " + _table.getTable().getName() +
126           " set " + _table.getValueColumn() + "=?" +
127           " where " + _table.getKeyColumn() + "=? " +
128           " and " + _table.getValueColumn() + "=?");
129     
130     DataSource JavaDoc ds = amberPersistenceUnit.getDataSource();
131     Connection conn = ds.getConnection();
132     try {
133       try {
134     PreparedStatement JavaDoc pstmt = conn.prepareStatement(_selectSQL);
135
136     pstmt.setString(1, _name);
137
138     ResultSet JavaDoc rs = pstmt.executeQuery();
139     if (rs.next()) {
140       rs.close();
141       return;
142     }
143       } catch (SQLException JavaDoc e) {
144       }
145
146       String JavaDoc sql = ("INSERT INTO " + _table.getTable().getName() + " (" +
147             _table.getKeyColumn() + "," +
148             _table.getValueColumn() + ") VALUES " +
149             "('" + _name + "', 1)");
150
151       Statement JavaDoc stmt = conn.createStatement();
152       stmt.executeUpdate(sql);
153       stmt.close();
154     } finally {
155       conn.close();
156     }
157   }
158 }
159
Popular Tags