01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: * $Source: /opt/cvs/rice/shared/src/main/java/org/apache/ojb/broker/platforms/KualiMySQLSequenceManagerImpl.java,v $
05: *
06: * Licensed under the Educational Community License, Version 1.0 (the "License")
07: ;
08: * you may not use this file except in compliance with the License.
09: * You may obtain a copy of the License at
10: *
11: * http://www.opensource.org/licenses/ecl1.php
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: package org.apache.ojb.broker.platforms;
20:
21: import java.sql.Connection;
22: import java.sql.ResultSet;
23: import java.sql.Statement;
24:
25: import org.apache.ojb.broker.PersistenceBroker;
26: import org.apache.ojb.broker.metadata.FieldDescriptor;
27: import org.apache.ojb.broker.util.sequence.AbstractSequenceManager;
28: import org.apache.ojb.broker.util.sequence.SequenceManagerException;
29:
30: public class KualiMySQLSequenceManagerImpl extends
31: AbstractSequenceManager {
32:
33: public KualiMySQLSequenceManagerImpl(PersistenceBroker broker) {
34: super (broker);
35: }
36:
37: @Override
38: protected long getUniqueLong(FieldDescriptor arg0)
39: throws SequenceManagerException {
40: PersistenceBroker broker = getBrokerForClass();
41:
42: Statement stmt = null;
43: Long seqNumber = null;
44: try {
45: Connection c = broker.serviceConnectionManager()
46: .getConnection();
47: stmt = c.createStatement();
48: String sql = "INSERT INTO " + arg0.getSequenceName()
49: + " VALUES (NULL);";
50: stmt.executeUpdate(sql);
51: sql = "SELECT LAST_INSERT_ID()";
52: ResultSet rs = stmt.executeQuery(sql);
53: if (rs != null) {
54: rs.first();
55: seqNumber = rs.getLong(1);
56: }
57: } catch (Exception e) {
58: throw new RuntimeException("Unable to execute: "
59: + e.getMessage());
60: } finally {
61: try {
62: if (stmt != null) {
63: stmt.close();
64: }
65: } catch (Exception e) {
66: throw new RuntimeException(
67: "Unable to close connection: " + e.getMessage());
68: }
69: }
70:
71: return seqNumber;
72: }
73:
74: }
|