1 43 package org.exolab.jms.persistence; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 49 50 57 public class SeedGenerator { 58 59 62 private static SeedGenerator _instance; 63 64 68 private static final Object _block = new Object (); 69 70 71 79 public static SeedGenerator instance() { 80 return _instance; 81 } 82 83 88 public static SeedGenerator initialise() { 89 if (_instance == null) { 90 synchronized (_block) { 91 if (_instance == null) { 92 _instance = new SeedGenerator(); 93 } 94 } 95 } 96 97 return _instance; 98 } 99 100 108 public synchronized long next(Connection connection, String name) 109 throws PersistenceException { 110 111 PreparedStatement select = null; 112 PreparedStatement update = null; 113 PreparedStatement insert = null; 114 ResultSet result = null; 115 long value = 0; 116 117 boolean successful = false; 118 try { 119 select = connection.prepareStatement( 120 "select seed from seeds where name=?"); 121 select.setString(1, name); 122 123 result = select.executeQuery(); 124 if (result.next()) { 125 value = result.getLong(1); 126 value++; 127 update = connection.prepareStatement( 128 "update seeds set seed=? where name=?"); 129 update.setLong(1, value); 130 update.setString(2, name); 131 update.executeUpdate(); 132 } else { 133 value = 1; 134 insert = connection.prepareStatement( 135 "insert into seeds (name, seed) values (?,?)"); 136 insert.setString(1, name); 137 insert.setLong(2, value); 138 insert.executeUpdate(); 139 } 140 } catch (Exception exception) { 141 throw new PersistenceException("next " + exception.getMessage()); 142 } finally { 143 SQLHelper.close(result); 144 SQLHelper.close(select); 145 SQLHelper.close(update); 146 SQLHelper.close(insert); 147 } 148 149 return value; 150 } 151 152 159 public void removeAll(Connection connection) 160 throws PersistenceException { 161 162 PreparedStatement delete = null; 163 try { 164 delete = connection.prepareStatement("delete from seeds"); 165 delete.executeUpdate(); 166 } catch (Exception error) { 167 throw new PersistenceException("Failed in removeAll with " + 168 error.toString()); 169 } finally { 170 SQLHelper.close(delete); 171 } 172 } 173 174 178 public void close() { 179 _instance = null; 180 } 181 182 185 protected SeedGenerator() { 186 } 187 } 188 | Popular Tags |