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