001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.idgen;
031:
032: import com.caucho.amber.manager.AmberConnection;
033: import com.caucho.amber.manager.AmberPersistenceUnit;
034: import com.caucho.amber.type.GeneratorTableType;
035: import com.caucho.util.L10N;
036: import com.caucho.util.Log;
037:
038: import javax.sql.DataSource;
039: import java.sql.Connection;
040: import java.sql.PreparedStatement;
041: import java.sql.ResultSet;
042: import java.sql.SQLException;
043: import java.sql.Statement;
044: import java.util.logging.Logger;
045:
046: /**
047: * Generator table.
048: */
049: public class AmberTableGenerator extends IdGenerator {
050: private static final L10N L = new L10N(AmberTableGenerator.class);
051: private static final Logger log = Log
052: .open(AmberTableGenerator.class);
053:
054: private AmberPersistenceUnit _manager;
055: private GeneratorTableType _table;
056: private String _name;
057:
058: private String _selectSQL;
059: private String _updateSQL;
060:
061: private boolean _isInit;
062:
063: /**
064: * Creates the table generator.
065: */
066: public AmberTableGenerator(AmberPersistenceUnit manager,
067: GeneratorTableType table, String name) {
068: _manager = manager;
069: _table = table;
070: _name = name;
071: }
072:
073: /**
074: * Allocates the next group of ids.
075: */
076: public long allocateGroup(AmberConnection aConn)
077: throws SQLException {
078: int groupSize = getGroupSize();
079:
080: int retry = 5;
081:
082: // XXX: should use non-XA
083: Connection conn = aConn.getConnection();
084: PreparedStatement selectStmt = conn
085: .prepareStatement(_selectSQL);
086: PreparedStatement updateStmt = conn
087: .prepareStatement(_updateSQL);
088:
089: selectStmt.setString(1, _name);
090: updateStmt.setString(2, _name);
091:
092: while (retry-- > 0) {
093: ResultSet rs = selectStmt.executeQuery();
094: if (rs.next()) {
095: long value = rs.getLong(1);
096: rs.close();
097:
098: updateStmt.setLong(1, value + groupSize);
099: updateStmt.setLong(3, value);
100:
101: if (updateStmt.executeUpdate() == 1)
102: return value;
103: }
104: rs.close();
105: }
106:
107: throw new SQLException(L.l(
108: "Can't allocate id from table '{0}'", _table.getTable()
109: .getName()));
110: }
111:
112: /**
113: * Initialize the table.
114: */
115: public void init(AmberPersistenceUnit amberPersistenceUnit)
116: throws SQLException {
117: if (_isInit)
118: return;
119: _isInit = true;
120:
121: _selectSQL = ("select " + _table.getValueColumn() + " from "
122: + _table.getTable().getName() + " where "
123: + _table.getKeyColumn() + "=?");
124:
125: _updateSQL = ("update " + _table.getTable().getName() + " set "
126: + _table.getValueColumn() + "=?" + " where "
127: + _table.getKeyColumn() + "=? " + " and "
128: + _table.getValueColumn() + "=?");
129:
130: DataSource ds = amberPersistenceUnit.getDataSource();
131: Connection conn = ds.getConnection();
132: try {
133: try {
134: PreparedStatement pstmt = conn
135: .prepareStatement(_selectSQL);
136:
137: pstmt.setString(1, _name);
138:
139: ResultSet rs = pstmt.executeQuery();
140: if (rs.next()) {
141: rs.close();
142: return;
143: }
144: } catch (SQLException e) {
145: }
146:
147: String sql = ("INSERT INTO " + _table.getTable().getName()
148: + " (" + _table.getKeyColumn() + ","
149: + _table.getValueColumn() + ") VALUES " + "('"
150: + _name + "', 1)");
151:
152: Statement stmt = conn.createStatement();
153: stmt.executeUpdate(sql);
154: stmt.close();
155: } finally {
156: conn.close();
157: }
158: }
159: }
|