1 19 20 package org.apache.avalon.excalibur.datasource.ids; 21 22 import java.math.BigDecimal ; 23 import java.sql.Connection ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 31 90 public class SequenceIdGenerator 91 extends AbstractDataSourceIdGenerator 92 { 93 private String m_query; 94 95 98 public SequenceIdGenerator() 99 { 100 } 101 102 105 113 protected BigDecimal getNextBigDecimalIdInner() 114 throws IdException 115 { 116 if( getLogger().isDebugEnabled() ) 117 { 118 getLogger().debug( "Requesting an Id using query: " + m_query ); 119 } 120 121 try 122 { 123 Connection conn = getConnection(); 124 try 125 { 126 PreparedStatement stmt = conn.prepareStatement( m_query ); 127 ResultSet rs = stmt.executeQuery(); 128 if( rs.next() ) 129 { 130 return rs.getBigDecimal( 1 ); 131 } 132 else 133 { 134 String msg = "Query for Id did not return a value"; 135 getLogger().error( msg ); 136 throw new IdException( msg ); 137 } 138 } 139 finally 140 { 141 conn.close(); 142 } 143 } 144 catch( SQLException e ) 145 { 146 String msg = "Unable to allocate an Id"; 147 getLogger().error( msg ); 148 throw new IdException( msg, e ); 149 } 150 } 151 152 160 protected long getNextLongIdInner() 161 throws IdException 162 { 163 if( getLogger().isDebugEnabled() ) 164 { 165 getLogger().debug( "Requesting an Id using query: " + m_query ); 166 } 167 168 try 169 { 170 Connection conn = getConnection(); 171 try 172 { 173 PreparedStatement stmt = conn.prepareStatement( m_query ); 174 ResultSet rs = stmt.executeQuery(); 175 if( rs.next() ) 176 { 177 return rs.getLong( 1 ); 178 } 179 else 180 { 181 String msg = "Query for Id did not return a value"; 182 getLogger().error( msg ); 183 throw new IdException( msg ); 184 } 185 } 186 finally 187 { 188 conn.close(); 189 } 190 } 191 catch( SQLException e ) 192 { 193 String msg = "Unable to allocate an Id"; 194 getLogger().error( msg ); 195 throw new IdException( msg, e ); 196 } 197 } 198 199 202 209 public void configure( Configuration configuration ) 210 throws ConfigurationException 211 { 212 super.configure( configuration ); 213 214 m_query = configuration.getChild( "query" ).getValue(); 216 } 217 } 218 219 | Popular Tags |