如果有人能在这里帮助我,我将不胜感激。我使用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 class 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文件做些什么。
有人会告诉我一些示例代码或者请给我一些帮助吗?
答案 0 :(得分:2)
我要做的第一件事是摆脱你自己的连接池代码并使用provided by the container。您基本上定义了DataSource,容器(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 / lib)。并发布你正在获得的堆栈跟踪。
更新2
错误很明显
部署URL文件的Web映射已存在 :/ C:/Users/Dane/Desktop/jboss-as-distribution-6.0.0.Final/jboss- 6.0.0.Final/server/default/tmp/vfs/automountec9d6360903186ac/SurveyApplication.war-a018e9cb945f462b /
似乎您已经使用相同的上下文路径部署了另一个应用程序。