使用tomcat服务器创建连接时出现以下错误。
org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法获取连接池错误超时等待空闲对象
我的JNDI配置如下:
<Resource name="jdbc/XXX"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://x.x.x.x:1433/ABC"
username="XXX"
password="XXX"
maxActive="30"
maxIdle="10"
maxWait="5000"
validationQuery="SELECT getDate()"
poolPreparedStatements="true" />
这是我的java代码。
package com.ef.clients.ptcl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.audium.server.AudiumException;
import com.audium.server.session.ActionElementData;
import com.audium.server.voiceElement.ActionElementBase;
public class ABC extends ActionElementBase {
DataSource ds;
public GetCallerCountNew(){
Context initialContext;
try{
initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("jdbc/XXX");
}catch(Exception ex){
}
}
CallableStatement proc = null;
Connection connection = null;
String ani = null;
String dnis = null;
@Override
public void doAction(String arg0, ActionElementData data) throws AudiumException {
try {
long time_1 = System.currentTimeMillis();
data.addToLog("Entered in getCallerCount class:",null);
ani = (String) data.getSessionData("ani");
dnis = (String) data.getSessionData("dnis");
//going to get connection.
if (ds == null) {
data.addToLog("DataSource isn't established.", null);
throw new AudiumException("DataSource isn't established.");
}
connection = ds.getConnection();
if (connection == null) {
data.addToLog("Connection isn't established.", null);
throw new AudiumException("Connection isn't established.");
}
// Reading Caller Count
int WirelineCountToday;
int WireLessCountToday;
int TotalCountToday;
proc = null;
proc = connection.prepareCall("{ call Custom_Db_Expertqueue_GetCallerCount(?,?,?,?,?) }");
data.addToLog("Setting ANI: ",ani);
String parameters = "ANI: " + ani + " DNIS: " + dnis;
data.addToLog("Calling Stroed proceedure with parameters . ", parameters);
proc.setString(1, ani);
proc.setString(2, dnis);
proc.registerOutParameter(3, java.sql.Types.INTEGER);
proc.registerOutParameter(4, java.sql.Types.INTEGER);
proc.registerOutParameter(5, java.sql.Types.INTEGER);
data.addToLog("Stroed proceedure executed successfully. ", null);
int out = proc.executeUpdate();
// getting output parameters from strored proceedure
WirelineCountToday=proc.getInt(3);// getInt(1);
WireLessCountToday=proc.getInt(4);
TotalCountToday=proc.getInt(5);
// setting session values
data.setSessionData("WirelineCount", WirelineCountToday);
data.setSessionData("WirelessCount", WireLessCountToday);
data.setSessionData("TotalCount", TotalCountToday);
data.addToLog("WirelineCountToday Value Retrieved:", Integer.toString(WirelineCountToday));
data.addToLog("TotalCountToday Value Retrieved:", Integer.toString(TotalCountToday));
data.addToLog("WireLessCountToday Value Retrieved:", Integer.toString(WireLessCountToday));
data.addToLog("Returned value from proceedure is:", Integer.toString(out));
data.addToLog("for Information.... SUCCESS = 0, FAILURE=1", null);
long time_2 = System.currentTimeMillis();
data.addToLog("Total Execution Time is: ", time_2-time_1+" ms");
}
catch (Exception e)
{ data.addToLog("Exeption while creating connection: ", e.toString()); }
finally {
data.addToLog("Going to close connection and callStatement.", null);
try {
if(proc !=null){
proc.close();
data.addToLog("callStatement Closed Successfully.", null);
}
else{
data.addToLog("callStatement is null..", null);
}
} catch (SQLException e) {
data.addToLog("ERROR WHILE TRYING TO CLOSE STATEMENT. MESSAGE IS: ", e.toString());
}
proc = null;
try {
if(connection != null) {
connection.close();
data.addToLog("Connection closed successfully.", null);
}
else{
data.addToLog("Connection is null..", null);
}
} catch (SQLException e) {
data.addToLog("ERROR WHILE TRYING TO CLOSE CONNECTINO. MESSAGE IS: ", e.toString());
}
connection = null;
}
}
}