?? databaseservice.java
字號:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: DatabaseService.java,v 1.1 2004/11/26 01:50:44 tanderson Exp $
*/
package org.exolab.jms.persistence;
import java.sql.Connection;
import javax.transaction.TransactionManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.DatabaseConfiguration;
import org.exolab.jms.config.RdbmsDatabaseConfiguration;
import org.exolab.jms.service.Service;
import org.exolab.jms.service.ServiceException;
/**
* The DatabaseService is used for managing the persistence aspect
* of this project.
*
* @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:44 $
* @author <a href="mailto:jima@intalio.com">Jim Alateras</a>
*/
public class DatabaseService extends Service {
/**
* The name of the service
*/
private final static String DB_SERVICE_NAME = "DatabaseService";
/**
* Maintains a singleton instance of the database service
*/
private static DatabaseService _instance = null;
/**
* Used to synchronize the creation of the database service
*/
private final static Object _creator = new Object();
/**
* Return a reference to the PersistenceAdaptor, that has been created
* by this service
*/
private PersistenceAdapter _adapter = null;
/**
* The logger
*/
private static final Log _log = LogFactory.getLog(DatabaseService.class);
/**
* Return the singleton instance of the DatabaseService
*
* @return DatabaseService
* @throws PersistenceException
*/
public static DatabaseService instance()
throws PersistenceException {
if (_instance == null) {
synchronized (_creator) {
// we need to check again if multiple threads
// have blocked on the creation of the singleton
if (_instance == null) {
_instance = new DatabaseService();
}
}
}
return _instance;
}
/**
* Return the {@link PersistenceAdapter} created by this service. This will
* always be non-null.
*
* @return PersistenceAdapter - the created adapter
*/
public static PersistenceAdapter getAdapter() {
return _instance._adapter;
}
/**
* Convenience function to return a connection to the PersistenceAdapter
*
* @return Connection - the connection
* @exception PersistenceException - if no connection could be retrieved
* or another error occured.
*/
public static Connection getConnection()
throws PersistenceException {
return getAdapter().getConnection();
}
/**
* Create an instance of a database service. It uses the configuration
* manager to extract the service parameters.
* <p>
* It will throw a PersistenceException, if it cannot construct the service
*
* @throws PersistenceException
*/
DatabaseService()
throws PersistenceException {
super(DB_SERVICE_NAME);
Configuration config = ConfigurationManager.getConfig();
DatabaseConfiguration dbconfig = config.getDatabaseConfiguration();
_adapter = createRdbmsAdapter(
dbconfig.getRdbmsDatabaseConfiguration());
}
// override Service.start
public void start() throws ServiceException {
super.start();
// remove the expired messages
Connection connection = null;
try {
connection = getConnection();
getAdapter().removeExpiredMessages(connection);
_log.info("Removed expired messages.");
connection.commit();
} catch (PersistenceException exception) {
SQLHelper.rollback(connection);
throw exception;
} catch (Exception exception) {
// rethrow as an appropriate exception
throw new ServiceException("Failed to start the DatabaseService",
exception);
} finally {
SQLHelper.close(connection);
}
}
// override Service.stop
public void stop()
throws ServiceException {
if (_adapter != null) {
_adapter.close();
}
synchronized (_creator) {
_instance = null;
}
super.stop();
}
/**
* Create an instance of an RDBMS persistence adapter using the specified
* database configuration. If it cannot create the adapter then throw
* {@lnik PersistenceException} exception
*
* @param config database configuration
* @return the created adapter
* @throws PersistenceException
*/
private PersistenceAdapter createRdbmsAdapter(
RdbmsDatabaseConfiguration config) throws PersistenceException {
PersistenceAdapter adapter = null;
if (config.hasBatch() && config.getBatch()) {
_log.info("Creating EXPERIMENTAL BatchingRdbmsAdapter for "
+ config.getDriver());
adapter = new BatchingRdbmsAdapter(
config.getDriver(),
config.getUrl(),
config.getUser(),
config.getPassword(),
config.getBatchSize());
} else {
_log.info("Creating RdbmsAdapter for " + config.getDriver());
adapter = new RDBMSAdapter(
config.getDriver(),
config.getUrl(),
config.getUser(),
config.getPassword());
}
return adapter;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -