001 /*
002 * Copyright 2005-2007 The Kuali Foundation
003 *
004 * $Source: /opt/cvs/rice/shared/src/main/java/org/apache/ojb/broker/platforms/KualiMySQLSequenceManagerImpl.java,v $
005 *
006 * Licensed under the Educational Community License, Version 2.0 (the "License")
007 ;
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.opensource.org/licenses/ecl2.php
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019 package org.apache.ojb.broker.platforms;
020
021 import java.sql.Connection;
022 import java.sql.ResultSet;
023 import java.sql.Statement;
024
025 import org.apache.ojb.broker.PersistenceBroker;
026 import org.apache.ojb.broker.metadata.FieldDescriptor;
027 import org.apache.ojb.broker.util.sequence.AbstractSequenceManager;
028 import org.apache.ojb.broker.util.sequence.SequenceManagerException;
029
030 public class KualiMySQLSequenceManagerImpl extends AbstractSequenceManager {
031
032 public KualiMySQLSequenceManagerImpl(PersistenceBroker broker) {
033 super(broker);
034 }
035
036 @Override
037 protected long getUniqueLong(FieldDescriptor arg0)
038 throws SequenceManagerException {
039 PersistenceBroker broker = getBrokerForClass();
040
041 Statement stmt = null;
042 Long seqNumber = null;
043 final String sequenceName = arg0.getSequenceName();
044
045 try {
046 //FIXME: should we be closing this connection in a finally block?
047 Connection c = broker.serviceConnectionManager().getConnection();
048 stmt = c.createStatement();
049 String sql = "INSERT INTO " + sequenceName + " VALUES (NULL);";
050 stmt.executeUpdate(sql);
051 sql = "SELECT LAST_INSERT_ID()";
052
053 //FIXME: should we be closing this result set in a finally block?
054 ResultSet rs = stmt.executeQuery(sql);
055 if (rs != null) {
056 rs.first();
057 seqNumber = rs.getLong(1);
058 }
059 } catch (Exception e) {
060 throw new RuntimeException("Unable to execute for sequence name: " + sequenceName, e);
061 } finally {
062 try {
063 if (stmt != null) {
064 stmt.close();
065 }
066 } catch (Exception e) {
067 throw new RuntimeException("Unable to close statement for sequence name: " + sequenceName, e);
068 }
069 }
070
071 return seqNumber;
072 }
073
074 }