1 45 package org.exolab.jms.persistence; 46 47 import java.sql.Connection ; 48 49 import javax.transaction.TransactionManager ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 import org.exolab.jms.config.Configuration; 55 import org.exolab.jms.config.ConfigurationManager; 56 import org.exolab.jms.config.DatabaseConfiguration; 57 import org.exolab.jms.config.RdbmsDatabaseConfiguration; 58 import org.exolab.jms.service.Service; 59 import org.exolab.jms.service.ServiceException; 60 61 62 69 public class DatabaseService extends Service { 70 71 74 private final static String DB_SERVICE_NAME = "DatabaseService"; 75 76 79 private static DatabaseService _instance = null; 80 81 84 private final static Object _creator = new Object (); 85 86 90 private PersistenceAdapter _adapter = null; 91 92 95 private static final Log _log = LogFactory.getLog(DatabaseService.class); 96 97 98 104 public static DatabaseService instance() 105 throws PersistenceException { 106 if (_instance == null) { 107 synchronized (_creator) { 108 if (_instance == null) { 111 _instance = new DatabaseService(); 112 } 113 } 114 } 115 116 return _instance; 117 } 118 119 125 public static PersistenceAdapter getAdapter() { 126 return _instance._adapter; 127 } 128 129 136 public static Connection getConnection() 137 throws PersistenceException { 138 return getAdapter().getConnection(); 139 } 140 141 149 DatabaseService() 150 throws PersistenceException { 151 super(DB_SERVICE_NAME); 152 153 Configuration config = ConfigurationManager.getConfig(); 154 DatabaseConfiguration dbconfig = config.getDatabaseConfiguration(); 155 156 _adapter = createRdbmsAdapter( 157 dbconfig.getRdbmsDatabaseConfiguration()); 158 } 159 160 public void start() throws ServiceException { 162 super.start(); 163 164 Connection connection = null; 166 try { 167 connection = getConnection(); 168 169 getAdapter().removeExpiredMessages(connection); 170 _log.info("Removed expired messages."); 171 connection.commit(); 172 173 } catch (PersistenceException exception) { 174 SQLHelper.rollback(connection); 175 throw exception; 176 } catch (Exception exception) { 177 throw new ServiceException("Failed to start the DatabaseService", 179 exception); 180 } finally { 181 SQLHelper.close(connection); 182 } 183 } 184 185 public void stop() 187 throws ServiceException { 188 189 if (_adapter != null) { 190 _adapter.close(); 191 } 192 193 synchronized (_creator) { 194 _instance = null; 195 } 196 197 super.stop(); 198 } 199 200 209 private PersistenceAdapter createRdbmsAdapter( 210 RdbmsDatabaseConfiguration config) throws PersistenceException { 211 212 PersistenceAdapter adapter = null; 213 214 if (config.hasBatch() && config.getBatch()) { 215 _log.info("Creating EXPERIMENTAL BatchingRdbmsAdapter for " 216 + config.getDriver()); 217 adapter = new BatchingRdbmsAdapter( 218 config.getDriver(), 219 config.getUrl(), 220 config.getUser(), 221 config.getPassword(), 222 config.getBatchSize()); 223 } else { 224 _log.info("Creating RdbmsAdapter for " + config.getDriver()); 225 adapter = new RDBMSAdapter( 226 config.getDriver(), 227 config.getUrl(), 228 config.getUser(), 229 config.getPassword()); 230 } 231 232 return adapter; 233 } 234 235 } 236 237 238 | Popular Tags |