KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.idgen;
30
31 import com.caucho.amber.manager.AmberConnection;
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.config.ConfigException;
34 import com.caucho.jdbc.JdbcMetaData;
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.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Generator table.
49  */

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

65   public SequenceIdGenerator(AmberPersistenceUnit manager,
66                              String JavaDoc name,
67                              int size)
68     throws ConfigException
69   {
70     _manager = manager;
71     _name = name;
72     _size = size;
73   }
74
75   /**
76    * Allocates the next group of ids.
77    */

78   public long allocateGroup(AmberConnection aConn)
79     throws SQLException JavaDoc
80   {
81     // XXX: should use non-XA
82
Connection conn = aConn.getConnection();
83
84     PreparedStatement JavaDoc selectStmt = conn.prepareStatement(_selectSQL);
85
86     long value = -1;
87
88     ResultSet JavaDoc rs = selectStmt.executeQuery();
89     if (rs.next())
90       value = rs.getLong(1);
91
92     rs.close();
93
94     return value;
95   }
96
97   /**
98    * Initialize the table.
99    */

100   public void init(AmberPersistenceUnit amberPersistenceUnit)
101     throws SQLException JavaDoc
102   {
103     if (_isInit)
104       return;
105     _isInit = true;
106
107     DataSource JavaDoc ds = amberPersistenceUnit.getDataSource();
108     Connection conn = ds.getConnection();
109     try {
110       JdbcMetaData metaData = amberPersistenceUnit.getMetaData();
111
112       _selectSQL = metaData.selectSequenceSQL(_name);
113
114       if (amberPersistenceUnit.getCreateDatabaseTables()) {
115         String JavaDoc sql = metaData.createSequenceSQL(_name, getGroupSize());
116
117         try {
118           Statement JavaDoc stmt = conn.createStatement();
119           stmt.executeUpdate(sql);
120           stmt.close();
121         } catch (Exception JavaDoc e) {
122           log.log(Level.FINER, e.toString(), e);
123         }
124       }
125     } finally {
126       conn.close();
127     }
128   }
129 }
130
Popular Tags