Tomcat ELB连接的Java/MySQL数据库问题
下面是连接到数据库的java代码:Tomcat ELB连接的Java/MySQL数据库问题,java,mysql,database-connection,Java,Mysql,Database Connection,下面是连接到数据库的java代码: public class DBConnectionFactory { private static String dbName = "TEST"; public static Connection getConnection(Connection conn) { Context initContext; try { initContext
public class DBConnectionFactory {
private static String dbName = "TEST";
public static Connection getConnection(Connection conn) {
Context initContext;
try {
initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/" + DBConnectionFactory.dbName);
conn = ds.getConnection();
return conn;
}
public static synchronized void returnConnection(Connection c) {
try {
if (c != null)
c.close();
}
}
Context.xml:
<Context>
<Resource name="jdbc/Test" auth="Container" type="javax.sql.DataSource"
username="test" password="test" driverClassName="com.mysql.jdbc.Driver"
maxActive="100" maxIdle="30" maxWait="10000" closeMethod="close"
validationQuery="SELECT 1"
url="jdbc:mysql://localhost:3306/Test?autoReconnect=true"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" />
</Context>
问题:应用程序运行在一台tomcat服务器上,我们没有问题。
通过对数据库服务器的两个连接进行负载平衡,我们得到以下错误:
通信链路故障\n\n发送到服务器的最后一个数据包是1毫秒前的。
锁定等待超时错误
请帮忙。我们可以有两个从ELB tomcat服务器到DB的池连接吗
谢谢
public class test {
CallableStatement cStmt = null;
ResultSet rs1 = null;
String uuId = getUuid();
Connection conn = null;
try {
conn = DBConnectionFactory.getConnection(conn);
cStmt = conn.prepareCall(
"{call ap_type_test(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
cStmt.setInt(1, parameters.getAdID());
cStmt.setString(2, parameters.getCode());
cStmt.execute();
status = cStmt.getString(19);
result = cStmt.getString(20);
} finally {
try {
if (rs1 != null) {
rs1.close();
}
try {
if (cStmt != null) {
cStmt.close();
}
try {
DBConnectionFactory.returnConnection(conn);
if (!conn.isClosed()) {
conn.close();
}
}
return serviceResponse;
}