View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 */ 43 44 package org.exolab.jms.persistence; 45 46 47 import java.sql.Connection; 48 import java.sql.PreparedStatement; 49 import java.sql.ResultSet; 50 51 import javax.sql.DataSource; 52 53 54 /*** 55 * This class generates seeds 56 * 57 * @version $Revision: 1.6 $ $Date: 2004/01/11 02:35:45 $ 58 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a> 59 * 60 **/ 61 public class SeedGenerator { 62 63 /*** 64 * Caches the singleton instance of this class 65 */ 66 private static SeedGenerator _instance; 67 68 /*** 69 * Monitor used to synchronize the initialization of the 70 * singleton class instance 71 */ 72 private static final Object _block = new Object(); 73 74 75 /*** 76 * Returns the singleton instance. 77 * 78 * Note that initialise() must have been invoked first for this 79 * to return a valid instance. 80 * 81 * @return SeedGenerator the singleton instance 82 */ 83 public static SeedGenerator instance() { 84 return _instance; 85 } 86 87 /*** 88 * Initialise the seed generator 89 * 90 * @return SeedGenerator the singleton instance 91 */ 92 public static SeedGenerator initialise() { 93 if (_instance == null) { 94 synchronized (_block) { 95 if (_instance == null) { 96 _instance = new SeedGenerator(); 97 } 98 } 99 } 100 101 return _instance; 102 } 103 104 /*** 105 * Return the next seed value for the given name 106 * 107 * @param connection - the connection used 108 * @param seed - the name of the seed 109 * @return long - the next seed value 110 * @exception PersistenceException - if the seed cannot be retrieved 111 */ 112 public synchronized long next(Connection connection, String name) 113 throws PersistenceException { 114 115 PreparedStatement select = null; 116 PreparedStatement update = null; 117 PreparedStatement insert = null; 118 ResultSet result = null; 119 long value = 0; 120 121 boolean successful = false; 122 try { 123 select = connection.prepareStatement( 124 "select * from seeds where name=?"); 125 select.setString(1, name); 126 127 result = select.executeQuery(); 128 if (result.next()) { 129 value = result.getLong("seed"); 130 value++; 131 update = connection.prepareStatement( 132 "update seeds set seed=? where name=?"); 133 update.setLong(1, value); 134 update.setString(2, name); 135 update.executeUpdate(); 136 } else { 137 value = 1; 138 insert = connection.prepareStatement( 139 "insert into seeds values (?,?)"); 140 insert.setString(1, name); 141 insert.setLong(2, value); 142 insert.executeUpdate(); 143 } 144 } catch (Exception exception) { 145 throw new PersistenceException("next " + exception.getMessage()); 146 } finally { 147 SQLHelper.close(select); 148 SQLHelper.close(update); 149 SQLHelper.close(insert); 150 SQLHelper.close(result); 151 } 152 153 return value; 154 } 155 156 /*** 157 * Remove all seeds. 158 * 159 * @param connection - the connection to use 160 * @exception PersistenceException - is thrown if the request does not 161 * complete 162 */ 163 public void removeAll(Connection connection) 164 throws PersistenceException { 165 166 PreparedStatement delete = null; 167 try { 168 delete = connection.prepareStatement("delete from seeds"); 169 delete.executeUpdate(); 170 } catch (Exception error) { 171 throw new PersistenceException("Failed in removeAll with " + 172 error.toString()); 173 } finally { 174 SQLHelper.close(delete); 175 } 176 } 177 178 /*** 179 * Issue a close, which in this case invalidates the singleton 180 * TODO: do we really need this 181 */ 182 public void close() { 183 _instance = null; 184 } 185 186 /*** 187 * Constructor 188 * 189 * @param connectionPool the connection pool 190 * @param manager the transaction manager 191 */ 192 protected SeedGenerator() { 193 } 194 }

This page was automatically generated by Maven