jboss接続池の配置
一、中間部品の配置
主にJBOSSサーバーのserver/default/deployディレクトリの下でoracle-ds.xmlファイルを配置します。主なパラメータの説明: local-tx-datasource:ローカルデータソース構成のルートタグの記名; jndi-name:データソースJNDI名前は、Apple.propertiesファイルに対応しています。 driver-class:データベース接続ドライバクラス; connection-url:データベース接続URL文字列; user-name:データベース接続ユーザ名; password:データベース接続パスワード; min-pool-size:接続池は最小接続数をアクティブにすることができます。 max-pool-size:接続池は最大接続数をアクティブにすることができます。 blocking-timeout-milis:例外を出す前に最大の待ち時間、単位ミリ秒; idle-timeout-minutes:接続プールがアクティブになっている空き接続のタイムアウト時間は、単位秒です。 二、事務管理メカニズム EJB容器初期化データ恒久化環境Session Contect; ユーザは、あるxxSession Beanにおけるトランザクション方法を呼び出す。 xSession Beanはある業務ロジックxLogicを呼び出して操作します。 xxLogicはDAO層アクセスデータを呼び出します。 2~4の過程で異常が発生した場合、session Contect呼び出し方法setRollbbbackonlyは異常を捕捉し、その異常をEJBException(RuntimeExceptionから継承)に転化して投げ続け、同時に事務をロールバックする。 三、事務管理の配置
JBOSSのEJB容器管理事務は、一般的にJBOSSのserver/default/confディレクトリの下に配置されています。具体的な配置パラメータは以下の通りです。 name=「jboss:service=Transation Manager」 xmboean-dd=「resorce:xmdesc/Transation ManagerService-xmboean.xml」 300 true jboss:service=XidFactory
四、JAVAコードはデータベース接続を取得する。
主にJBOSSサーバーのserver/default/deployディレクトリの下でoracle-ds.xmlファイルを配置します。主なパラメータの説明:
JBOSSのEJB容器管理事務は、一般的にJBOSSのserver/default/confディレクトリの下に配置されています。具体的な配置パラメータは以下の通りです。 name=「jboss:service=Transation Manager」 xmboean-dd=「resorce:xmdesc/Transation ManagerService-xmboean.xml」 300 true jboss:service=XidFactory
四、JAVAコードはデータベース接続を取得する。
package com.sunrise.www.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sunrise.www.utils.exception.DAOException;
public class DAOUtils {
private static final Log log = LogFactory.getLog(DAOUtils.class);
private static boolean datasource_flag = false;
private static boolean read_datasource_flag = true;
private static boolean read_direct_conn_flag = false;
private static String datasource_name = null;
public static Connection getDBConnection(String datasourceName)
throws DAOException {
try {
return ServiceLocator.getInstance().getDataSource(datasourceName).getConnection();
} catch (Exception ex) {
throw new DAOException("Cannot connect to database.", ex);
}
}
public static Connection getDBConnection() throws DAOException {
try {
if (read_datasource_flag) {
String tmp = SystemConfig.getDefaultConfig("USE_DATASOURCE").toLowerCase();
datasource_flag = Boolean.parseBoolean(tmp);
read_datasource_flag = false;
}
Connection conn = null;
if (!datasource_flag) {
String user = "", password = "", url = "", driver = "";
if (!read_direct_conn_flag) {
user = SystemConfig.getDefaultConfig("USER");
password = SystemConfig.getDefaultConfig("PASSWORD");
url = SystemConfig.getDefaultConfig("URL");
driver = SystemConfig.getDefaultConfig("DATABASEDRIVER");
read_direct_conn_flag = true;
}
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(true);
} else {
if (datasource_name == null)
datasource_name = SystemConfig.getDefaultConfig("DATASOURCE_NAME");
conn = getDBConnection(datasource_name);
}
return conn;
} catch (Exception ex) {
log.error(" ", ex);
throw new DAOException(ex);
}
}
public static void closeAll(Connection dbConnection,
PreparedStatement stmt, ResultSet result) throws DAOException {
close(result);
close(stmt);
close(dbConnection);
}
public static void close(Connection dbConnection, PreparedStatement stmt)
throws DAOException {
close(stmt);
close(dbConnection);
}
public static void close(Connection dbConnection) throws DAOException {
try {
if (dbConnection != null && !dbConnection.isClosed()) {
dbConnection.close();
dbConnection = null;
}
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "DB connection : /n", se);
}
}
public static void close(ResultSet result) throws DAOException {
try {
if (result != null) result.close();
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "Result Set : /n", se);
}
}
public static void close(PreparedStatement stmt) throws DAOException {
try {
if (stmt != null) stmt.close();
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "Statement : /n", se);
}
}
/**
* @return read_datasource_flag
*/
public static boolean isRead_datasource_flag() {
return read_datasource_flag;
}
/**
* @param read_datasource_flag
* read_datasource_flag
*/
public static void setRead_datasource_flag(boolean read_datasource_flag) {
DAOUtils.read_datasource_flag = read_datasource_flag;
}
}
package com.sunrise.www.utils;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sunrise.www.utils.exception.ServiceLocatorException;
public class ServiceLocator {
private static Log log = LogFactory.getLog(ServiceLocator.class);
private static ServiceLocator serviceLocator;
private static InitialContext context;
private static Hashtable datasourceTable = new Hashtable();
private ServiceLocator() throws ServiceLocatorException {
try {
context = getInitialContext();
} catch (Exception e) {
log.error(e.getMessage());
throw new ServiceLocatorException(e.getMessage());
}
}
/**
*
* @return
* @throws ServiceLocatorException
*/
public static synchronized ServiceLocator getInstance()
throws ServiceLocatorException {
if (serviceLocator == null)
serviceLocator = new ServiceLocator();
return serviceLocator;
}
/**
* J2EE
* @return
* @throws NamingException
*/
public static InitialContext getInitialContext() throws NamingException {
return new InitialContext();
}
/**
* data source
* @param datasourceName
* @return
* @throws ServiceLocatorException
*/
public DataSource getDataSource(String datasourceName)
throws ServiceLocatorException {
try {
DataSource dataSource = (DataSource) datasourceTable.get(datasourceName);
if (dataSource == null) {
dataSource = (DataSource) context.lookup(actualJndiName(datasourceName));
if (dataSource == null) {
log.error(" : " + datasourceName);
throw new ServiceLocatorException(" : " + datasourceName);
}
datasourceTable.put(datasourceName, dataSource);
}
return dataSource;
} catch (NamingException ne) {
log.error(ne.getMessage());
throw new ServiceLocatorException(ne);
}
}
private String actualJndiName(String jndiName) throws NamingException {
String prefixes = (String) context.getEnvironment().get(InitialContext.URL_PKG_PREFIXES);
return ((prefixes != null && prefixes.contains("jboss")) ? "java:/" : "") + jndiName;
}
}
package com.sunrise.www.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.sunrise.www.utils.exception.SystemConfigException;
public class SystemConfig {
private static final String DEFAULT_CONFIG = "Application.properties";
private static ClassLoader defaultClassLoader;
private SystemConfig() {
}
/**
* Returns the default classloader (may be null).
*
* @return The default classloader
*/
public static ClassLoader getDefaultClassLoader() {
return defaultClassLoader;
}
/**
* Sets the default classloader
*
* @param defaultClassLoader -
* the new default ClassLoader
*/
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
SystemConfig.defaultClassLoader = defaultClassLoader;
}
/**
* Returns a resource on the classpath as a Properties object
*
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static Properties getResourceAsProperties(String resource)
throws IOException {
Properties props = new Properties();
InputStream in = null;
String propfile = resource;
in = getResourceAsStream(propfile);
props.load(in);
in.close();
return props;
}
/**
* Returns a resource on the classpath as a Properties object
*
* @param loader
* The classloader used to load the resource
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static Properties getResourceAsProperties(ClassLoader loader,
String resource) throws IOException {
Properties props = new Properties();
InputStream in = null;
String propfile = resource;
in = getResourceAsStream(loader, propfile);
props.load(in);
in.close();
return props;
}
/**
* Returns a resource on the classpath as a Stream object
*
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(String resource)
throws IOException {
return getResourceAsStream(getClassLoader(), resource);
}
/**
* Returns a resource on the classpath as a Stream object
*
* @param loader
* The classloader used to load the resource
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(ClassLoader loader,
String resource) throws IOException {
InputStream in = null;
if (loader != null)
in = loader.getResourceAsStream(resource);
if (in == null)
in = ClassLoader.getSystemResourceAsStream(resource);
if (in == null)
throw new IOException("Could not find resource " + resource);
return in;
}
private static ClassLoader getClassLoader() {
if (defaultClassLoader != null) {
return defaultClassLoader;
} else {
return Thread.currentThread().getContextClassLoader();
}
}
// com.ibatis.common.resources.Resources
/**
* (Application.properties)
*
* @param key
*
* @param defaultValue
*
* @return
*/
public static String getDefaultConfig(String key, String defaultValue) {
return loadProperties(DEFAULT_CONFIG).getProperty(key, defaultValue);
}
public static String getDefaultConfig(String key) {
return loadProperties(DEFAULT_CONFIG).getProperty(key);
}
public static String getConfig(String resource, String key) {
return loadProperties(resource).getProperty(key);
}
public static String getConfig(String resource, String key,
String defaultValue) {
return loadProperties(resource).getProperty(key);
}
public static Properties loadProperties(String resource) {
try {
Properties props = getResourceAsProperties(resource);
if(props != null){
return props;
}
throw new SystemConfigException("Resource " + resource + " is not exist.");
} catch (IOException e) {
throw new SystemConfigException(e);
}
}
}