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: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.idgen;
030:
031: import com.caucho.amber.manager.AmberConnection;
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.config.ConfigException;
034: import com.caucho.jdbc.JdbcMetaData;
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.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * Generator table.
049: */
050: public class SequenceIdGenerator extends IdGenerator {
051: private static final L10N L = new L10N(SequenceIdGenerator.class);
052: private static final Logger log = Log
053: .open(SequenceIdGenerator.class);
054:
055: private AmberPersistenceUnit _manager;
056: private String _name;
057: private int _size;
058:
059: private String _selectSQL;
060:
061: private boolean _isInit;
062:
063: /**
064: * Creates the table generator.
065: */
066: public SequenceIdGenerator(AmberPersistenceUnit manager,
067: String name, int size) throws ConfigException {
068: _manager = manager;
069: _name = name;
070: _size = size;
071: }
072:
073: /**
074: * Allocates the next group of ids.
075: */
076: public long allocateGroup(AmberConnection aConn)
077: throws SQLException {
078: // XXX: should use non-XA
079: Connection conn = aConn.getConnection();
080:
081: PreparedStatement selectStmt = conn
082: .prepareStatement(_selectSQL);
083:
084: long value = -1;
085:
086: ResultSet rs = selectStmt.executeQuery();
087: if (rs.next())
088: value = rs.getLong(1);
089:
090: rs.close();
091:
092: return value;
093: }
094:
095: /**
096: * Initialize the table.
097: */
098: public void init(AmberPersistenceUnit amberPersistenceUnit)
099: throws SQLException {
100: if (_isInit)
101: return;
102: _isInit = true;
103:
104: DataSource ds = amberPersistenceUnit.getDataSource();
105: Connection conn = ds.getConnection();
106: try {
107: JdbcMetaData metaData = amberPersistenceUnit.getMetaData();
108:
109: _selectSQL = metaData.selectSequenceSQL(_name);
110:
111: if (amberPersistenceUnit.getCreateDatabaseTables()) {
112: String sql = metaData.createSequenceSQL(_name,
113: getGroupSize());
114:
115: try {
116: Statement stmt = conn.createStatement();
117: stmt.executeUpdate(sql);
118: stmt.close();
119: } catch (Exception e) {
120: log.log(Level.FINER, e.toString(), e);
121: }
122: }
123: } finally {
124: conn.close();
125: }
126: }
127: }
|