View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.ojb.broker.platforms;
17  
18  import java.sql.Connection;
19  import java.sql.ResultSet;
20  import java.sql.Statement;
21  
22  import org.apache.ojb.broker.PersistenceBroker;
23  import org.apache.ojb.broker.metadata.FieldDescriptor;
24  import org.apache.ojb.broker.util.sequence.AbstractSequenceManager;
25  import org.apache.ojb.broker.util.sequence.SequenceManagerException;
26  
27  @Deprecated
28  public class KualiMySQLSequenceManagerImpl extends AbstractSequenceManager {
29  
30  	public KualiMySQLSequenceManagerImpl(PersistenceBroker broker) {
31  		super(broker);
32  	}
33  
34  	@Override
35  	protected long getUniqueLong(FieldDescriptor arg0)
36  			throws SequenceManagerException {
37  		PersistenceBroker broker = getBrokerForClass();
38  
39  		Statement stmt = null;
40  		Long seqNumber = null;
41  		final String sequenceName = arg0.getSequenceName();
42  
43  		try {
44  			//FIXME: should we be closing this connection in a finally block?
45  			Connection c = broker.serviceConnectionManager().getConnection();
46  			stmt = c.createStatement();
47  			String sql = "INSERT INTO " + sequenceName + " VALUES (NULL);";
48  			stmt.executeUpdate(sql);
49  			sql = "SELECT LAST_INSERT_ID()";
50  			
51  			//FIXME: should we be closing this result set in a finally block?
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 for sequence name: " + sequenceName, e);
59  		} finally {
60  			try {
61  				if (stmt != null) {
62  					stmt.close();
63  				}
64  			} catch (Exception e) {
65  				throw new RuntimeException("Unable to close statement for sequence name: " + sequenceName, e);
66  			}
67  		}
68  
69  		return seqNumber;
70  	}
71  
72  }