1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }