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