1 package org.apache.ojb.broker.util.sequence; 2 3 17 18 import java.sql.ResultSet ; 19 import java.sql.Statement ; 20 import java.sql.PreparedStatement ; 21 22 import org.apache.commons.lang.SystemUtils; 23 import org.apache.ojb.broker.PersistenceBroker; 24 import org.apache.ojb.broker.accesslayer.StatementManagerIF; 25 import org.apache.ojb.broker.metadata.ClassDescriptor; 26 import org.apache.ojb.broker.metadata.FieldDescriptor; 27 import org.apache.ojb.broker.query.Query; 28 import org.apache.ojb.broker.util.logging.Logger; 29 import org.apache.ojb.broker.util.logging.LoggerFactory; 30 31 92 public class SequenceManagerNextValImpl extends AbstractSequenceManager 93 { 94 private Logger log = LoggerFactory.getLogger(SequenceManagerNextValImpl.class); 95 96 99 public SequenceManagerNextValImpl(PersistenceBroker broker) 100 { 101 super(broker); 102 } 103 104 108 protected int getUniqueId(FieldDescriptor field) throws SequenceManagerException 109 { 110 return (int) getUniqueLong(field); 111 } 112 113 114 118 protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException 119 { 120 long result; 121 String sequenceName = calculateSequenceName(field); 123 try 124 { 125 result = buildNextSequence(field.getClassDescriptor(), sequenceName); 126 } 127 catch (Throwable e) 128 { 129 try 131 { 132 log.info("Create DB sequence key '"+sequenceName+"'"); 133 createSequence(field.getClassDescriptor(), sequenceName); 134 } 135 catch (Exception e1) 136 { 137 throw new SequenceManagerException( 138 SystemUtils.LINE_SEPARATOR + 139 "Could not grab next id, failed with " + SystemUtils.LINE_SEPARATOR + 140 e.getMessage() + SystemUtils.LINE_SEPARATOR + 141 "Creation of new sequence failed with " + 142 SystemUtils.LINE_SEPARATOR + e1.getMessage() + SystemUtils.LINE_SEPARATOR 143 , e1); 144 } 145 try 146 { 147 result = buildNextSequence(field.getClassDescriptor(), sequenceName); 148 } 149 catch (Throwable e1) 150 { 151 throw new SequenceManagerException("Could not grab next id, sequence seems to exist", e); 152 } 153 } 154 return result; 155 } 156 157 protected long buildNextSequence(ClassDescriptor cld, String sequenceName) throws Exception 158 { 159 ResultSet rs = null; 160 PreparedStatement stmt = null; 161 long result = -1; 162 StatementManagerIF stmtMan = getBrokerForClass().serviceStatementManager(); 163 try 164 { 165 stmt = stmtMan.getPreparedStatement(cld, getPlatform().nextSequenceQuery(sequenceName) ,Query.NOT_SCROLLABLE, 1, false); 166 rs = stmt.executeQuery(); 167 rs.next(); 168 result = rs.getLong(1); 169 } 170 finally 171 { 172 stmtMan.closeResources(stmt, rs); 173 } 174 return result; 175 } 176 177 protected void createSequence(ClassDescriptor cld, String sequenceName) throws Exception 178 { 179 Statement stmt = null; 180 StatementManagerIF stmtMan = getBrokerForClass().serviceStatementManager(); 181 204 try 205 { 206 stmt = stmtMan.getGenericStatement(cld, Query.NOT_SCROLLABLE); 207 stmt.execute(getPlatform().createSequenceQuery(sequenceName, getConfigurationProperties())); 208 } 209 finally 210 { 211 try 212 { 213 stmtMan.closeResources(stmt, null); 214 } 215 catch (Exception e) 216 { 217 e.printStackTrace(); 218 } 219 } 220 } 221 } 222 | Popular Tags |