001/**
002 * Copyright 2005-2016 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
027@Deprecated
028public class KualiMySQLSequenceManagerImpl extends AbstractSequenceManager {
029
030        public KualiMySQLSequenceManagerImpl(PersistenceBroker broker) {
031                super(broker);
032        }
033
034        @Override
035        protected long getUniqueLong(FieldDescriptor arg0)
036                        throws SequenceManagerException {
037                PersistenceBroker broker = getBrokerForClass();
038
039                Statement stmt = null;
040                Long seqNumber = null;
041                final String sequenceName = arg0.getSequenceName();
042
043                try {
044                        //FIXME: should we be closing this connection in a finally block?
045                        Connection c = broker.serviceConnectionManager().getConnection();
046                        stmt = c.createStatement();
047                        String sql = "INSERT INTO " + sequenceName + " VALUES (NULL);";
048                        stmt.executeUpdate(sql);
049                        sql = "SELECT LAST_INSERT_ID()";
050                        
051                        //FIXME: should we be closing this result set in a finally block?
052                        ResultSet rs = stmt.executeQuery(sql);
053                        if (rs != null) {
054                                rs.first();
055                                seqNumber = rs.getLong(1);
056                        }
057                } catch (Exception e) {
058                        throw new RuntimeException("Unable to execute for sequence name: " + sequenceName, e);
059                } finally {
060                        try {
061                                if (stmt != null) {
062                                        stmt.close();
063                                }
064                        } catch (Exception e) {
065                                throw new RuntimeException("Unable to close statement for sequence name: " + sequenceName, e);
066                        }
067                }
068
069                return seqNumber;
070        }
071
072}