View Javadoc

1   /**
2    * Copyright 2005-2011 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  public class KualiMySQLSequenceManagerImpl extends AbstractSequenceManager {
28  
29  	public KualiMySQLSequenceManagerImpl(PersistenceBroker broker) {
30  		super(broker);
31  	}
32  
33  	@Override
34  	protected long getUniqueLong(FieldDescriptor arg0)
35  			throws SequenceManagerException {
36  		PersistenceBroker broker = getBrokerForClass();
37  
38  		Statement stmt = null;
39  		Long seqNumber = null;
40  		final String sequenceName = arg0.getSequenceName();
41  
42  		try {
43  			//FIXME: should we be closing this connection in a finally block?
44  			Connection c = broker.serviceConnectionManager().getConnection();
45  			stmt = c.createStatement();
46  			String sql = "INSERT INTO " + sequenceName + " VALUES (NULL);";
47  			stmt.executeUpdate(sql);
48  			sql = "SELECT LAST_INSERT_ID()";
49  			
50  			//FIXME: should we be closing this result set in a finally block?
51  			ResultSet rs = stmt.executeQuery(sql);
52  			if (rs != null) {
53  				rs.first();
54  				seqNumber = rs.getLong(1);
55  			}
56  		} catch (Exception e) {
57  			throw new RuntimeException("Unable to execute for sequence name: " + sequenceName, e);
58  		} finally {
59  			try {
60  				if (stmt != null) {
61  					stmt.close();
62  				}
63  			} catch (Exception e) {
64  				throw new RuntimeException("Unable to close statement for sequence name: " + sequenceName, e);
65  			}
66  		}
67  
68  		return seqNumber;
69  	}
70  
71  }