如果有人能帮助我,我将不胜感激。我已经使用 Apache Tomcat 开发了一个应用程序,它已经部署并且可以正常工作。但是,当我想将应用程序迁移到 JBoss 并在该服务器上部署 WAR 文件时,出现数据源错误。我是 JBoss 的新手,我不知道下一步该怎么做才能解决这个问题。如果有人可以指导我完成这个过程,我将不胜感激!
我的 DBConnector 类代码:
import java.sql.*;
import java.util.Properties;
import java.io.InputStream;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory;
公共(public)类 DbConnector {
private static String JDBC_DRIVER = "jdbc.driver";
private static String JDBC_URL = "jdbc.url";
private static String JDBC_USER = "jdbc.user";
private static String JDBC_PASSWORD = "jdbc.password";
private static Properties props = new Properties();
private Connection connection = null;
private Statement stat = null;
private ResultSet rs = null;
private static volatile DataSource dsObj;
static {
try {
// a way to retrieve the data in
// connection.properties found
// in WEB-INF/classes
InputStream is = DbConnector.class.getResourceAsStream("/connection.properties");
props.load(is);
//PropertyConfigurator.configure("log4j.properties");
} catch (Exception e) {
e.printStackTrace();
}
try {
Class.forName(props.getProperty(JDBC_DRIVER)).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void initialize() {
try {
dsObj = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Constructor
*/
public DbConnector() {
try {
initialize();
this.connection = getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns DB Connection
* @return Connection
* @throws SQLException
*/
public static Connection getConnectionFromPool() throws SQLException {
Connection connection = null;
// checking for null singleton instance
if (null == dsObj) { // synchronized over class to make thread safe
synchronized (DbConnector.class) {
// double checking for making singleton instance thread safe
if (null == dsObj) {
initialize();
}
}
}
// getting connection from data sourceconnection = dsObj.getConnection();
return connection;
}
/**
* Get Connection
* @return Connection object
* @throws SQLException
*/
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(props.getProperty(JDBC_URL), props.getProperty(JDBC_USER), props.getProperty(JDBC_PASSWORD));
}
/**
* Execute Query
* Purpose: SELECT
* @param sql SQL Statement
* @return ResultSet
*/
public ResultSet executeQuery(String sql) {
try {
if (connection == null) {
return null;
}
stat = connection.createStatement();
rs = stat.executeQuery(sql);
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Execute Update
* Purpose: Insert, Update, Delete
* @param sql SQL Statement
* @return int No. of Rows Updated
*/
public int executeUpdate(String sql) {
try {
if (connection == null) {
return -1;
}
stat = connection.createStatement();
return stat.executeUpdate(sql);
} catch (Exception e) {
//e.printStackTrace();
return -1;
}
}
/**
* Execute
* Purpose: Create, Drop
* @param sql statement to update.
* @return true is statement execute sucessfuly and false otherwise
*/
public boolean execute(String sql) {
try {
if (connection == null) {
return false;
}
stat = connection.createStatement();
return stat.execute(sql);
} catch (Exception e) {
//e.printStackTrace();
return false;
}
}
/**
* Close ResultSet
*/
public void closeResultSet() {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
}
/**
* Close Statement
*/
public void closeStatement() {
if (stat != null) {
try {
stat.close();
} catch (Exception e) {
e.printStackTrace();
//log.error(e);
}
}
}
/**
* Close Connection
*/
public void closeConnection() {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Close
* Connection, Statement and Resultset *
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stat != null) {
stat.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
在线阅读此内容后,我知道我必须对 context.xml 和 web.xml 文件执行一些操作。
有人可以给我一些示例代码或给我一些帮助吗?
最佳答案
我做的第一件事就是摆脱你自己的连接池代码并使用一个 provided by the container .您基本上定义了数据源,容器 (Tomcat/JBOSS) 将通过 JNDI 将其提供给您的应用程序。定义后,您可以在 web.xml 中引用它。在谷歌上搜索操作方法。
<resource-ref>
<description>Customer Database</description>
<res-ref-name>jdbc/CustomerDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
更新
如果您仍然想让它按照您的方式工作,那么请确保您的类路径中有包含 org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory
的 jar 文件(WEB-INF/库)。并发布您获得的堆栈跟踪信息。
更新 2
错误很明显
Web 映射已经存在 用于部署 URL 文件:/C:/Users/Dane/Desktop/jboss-as-distribution-6.0.0.Final/jboss- 6.0.0.Final/server/default/tmp/vfs/automountec9d6360903186ac/SurveyApplication.war-a018e9cb945f462b/
似乎您已经使用相同的上下文路径部署了另一个应用程序。
关于java - 从 Tomcat 服务器迁移到 JBoss 时数据源错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074999/