View Javadoc

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