您好我正在使用c3p0连接池:我的JDBC类:
public static Connection connect() {
Connection conn = null;
ComboPooledDataSource dataSource;
try{
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
dataSource.setJdbcUrl( "jdbc:mysql://localhost/circuit" );
dataSource.setUser("root");
dataSource.setPassword("");
dataSource.setAutoCommitOnClose(false);
dataSource.setMaxIdleTime(5);
// the settings below are optional -- c3p0 can work with defaults
//conn.setMinPoolSize(5);
//conn.setAcquireIncrement(5);
//conn.setMaxPoolSize(20);
conn = dataSource.getConnection();
conn.setAutoCommit(false);
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
return conn;
}//end try
}
然后我有servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BasicConfigurator.configure();
logger.info("Entered doGet method of AdvisorMyAccountRequestController");
int advisorId = 0;
String username = "";
//int[] id = new int[50];
//int[] userIds = new int[];
List<Integer> userIds = new ArrayList<Integer>();
//List<Integer> requestId = new ArrayList<Integer>();
int i= 0;
try{
advisorId = (int) request.getSession().getAttribute("advisorId");
username = (String) request.getSession().getAttribute("username");
}catch(Exception e){
response.sendRedirect("Error.jsp");
}
List<UserDetailsDTO> userDetailsList = new ArrayList<UserDetailsDTO>();
//List<UserRequestDTO> userRequestDetails = new ArrayList<UserRequestDTO>();
int userId = 0;
//int requestId = 0;
Timestamp bookingDate = null;
if(username != null && advisorId != 0 && !("").equals(username))
{
/* //Getting request Id from the session table where STATUS = "PENDING FOR USER PAYMENT" AND "PENDING FOR USER PAYMENT WITH NEW DATES"
MyAccountRequestDAO requestIdList = new MyAccountRequestDAO();
requestId = requestIdList.getRequestId(advisorId);
if(requestId != null && requestId.size() > 0) {
//Getting request details of the advisor from the 'session table'
MyAccountRequestDAO requestDetails = new MyAccountRequestDAO();
userRequestDetails = requestDetails.getRequestDetails(requestId);
for (UserRequestDTO userRequestDTO : userRequestDetails) {
userIds[i] = userRequestDTO.getUserId();
i++;
}
}
*/
//Getting request details of the advisor from the 'userrequest' where STATUS = "PENDING FOR ADVISOR APPROVAL" to display on the Request Tab of the Myaccount page.
MyAccountRequestDAO dao = new MyAccountRequestDAO();
List<UserRequestDTO> list = dao.getAdvisorRequestDetails(advisorId);
List<UserRequestDTO> list1 = new ArrayList<UserRequestDTO>();
List<TimeDTO> difference = new ArrayList<TimeDTO>();
Boolean isStatusCommit = false;
for (UserRequestDTO userRequestDTO : list) {
String status = userRequestDTO.getStatus();
if(("PENDING FOR ADVISOR APPROVAL").equals(status)){
userIds.add(userRequestDTO.getUserId()) ;
bookingDate = userRequestDTO.getBookingTime();
GetTimeLeftForReply time = new GetTimeLeftForReply();
difference = time.getHoursAndMinutes(bookingDate);
if(difference.size() > 0){
for (TimeDTO timeDTO : difference) {
userRequestDTO.setDays(timeDTO.getDay());
userRequestDTO.setHours(timeDTO.getHours());
userRequestDTO.setMinutes(timeDTO.getMinutes());
}
}else{
userRequestDTO.setDays(0);
userRequestDTO.setHours(0);
userRequestDTO.setMinutes(0);
/* //If advisor has lapsed the the time to reply, then change the status of the request.
String status1 = "SESSION LAPSE BY ADVISOR";
ChangeRequestStatusDAO change = new ChangeRequestStatusDAO();
isStatusCommit = change.setStatus(status1,requestId);
userRequestDTO.setStatus(status1); */
}
list1.add(userRequestDTO);
}else if (("REQUEST ACCEPTED BY ADVISOR").equals(status)) {
userIds.add(userRequestDTO.getUserId()) ;
userRequestDTO.setDays(0);
userRequestDTO.setHours(0);
userRequestDTO.setMinutes(0);
userRequestDTO.setStatus("PENDING FOR USER PAYMENT");
list1.add(userRequestDTO);
}else if (("REQUEST ACCEPTED BY ADVISOR WITH NEW DATES").equals(status)) {
userIds.add(userRequestDTO.getUserId()) ;
i++;
userRequestDTO.setDays(0);
userRequestDTO.setHours(0);
userRequestDTO.setMinutes(0);
userRequestDTO.setStatus("PENDING FOR USER PAYMENT WITH NEW DATES");
list1.add(userRequestDTO);
}
}
//Fetching the name of the user with id in int[] userIds.
if(userIds.size() > 0 ){
//Fetching user details from the uderdetails table
UserDetailsDAO user1 = new UserDetailsDAO();
userDetailsList = user1.getUserDetails(userIds);
}
request.setAttribute("list1", list1);
//request.setAttribute("userRequestDetails", userRequestDetails);
request.setAttribute("userDetailsList", userDetailsList);
request.setAttribute("type", "advisor");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/requests.jsp");
rd.forward(request, response);
}
我的一个DAO:
public List<UserRequestDTO> getRequestDetails(int userId){
BasicConfigurator.configure();
logger.info("Entered getRequestDetails method of MyAccountRequestDAO");
List<UserRequestDTO> list = new ArrayList<UserRequestDTO>();
PreparedStatement pstmt;
try {
conn =Util.connect();
String query ="SELECT * FROM userrequest WHERE USER_ID = ?";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, userId);
ResultSet results = pstmt.executeQuery();
while(results.next()){
UserRequestDTO user = new UserRequestDTO();
user.setRequestId(results.getInt("REQUEST_ID"));
user.setUserId(results.getInt("USER_ID"));
user.setAdvisorId(results.getInt("ADVISOR_ID"));
user.setService(results.getString("SERVICE"));
user.setMode(results.getString("MODE_OF_COMMUNICATION"));
user.setQuery(results.getString("QUERY"));
user.setDuration(results.getString("DURATION"));
user.setBookingTime(results.getTimestamp("BOOKING_TIME"));
user.setTime1(results.getTimestamp("DATE_TIME1"));
user.setTime2(results.getTimestamp("DATE_TIME2"));
user.setTime3(results.getTimestamp("DATE_TIME3"));
user.setTime4(results.getTimestamp("DATE_TIME4"));
user.setTime5(results.getTimestamp("DATE_TIME5"));
user.setTime6(results.getTimestamp("DATE_TIME6"));
user.setStatus(results.getString("STATUS"));
list.add(user);
}
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
logger.error("getRequestDetails method of MyAccountRequestDAO threw error:"+e.getMessage());
e1.printStackTrace();
}
logger.error("getRequestDetails method of MyAccountRequestDAO threw error:"+e.getMessage());
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
logger.error("getRequestDetails method of MyAccountRequestDAO threw error:"+e.getMessage());
e.printStackTrace();
}
}
logger.info("Exit getRequestDetails method of MyAccountRequestDAO");
return list;
}
当我调试代码时,servlet会一次又一次地执行。我猜它是在无限循环中的某个地方 这个错误一次又一次地弹出:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
at sun.reflect.GeneratedConstructorAccessor19.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1079)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:66)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:76)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:66)
at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:787)
at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:548)
at com.mchange.v2.resourcepool.BasicResourcePool.access$000(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:972)
at java.lang.Thread.run(Unknown Source)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7c1b964e -- Unexpectedly Broken!!!
While trying to acquire a needed new resource, we failed
to succeed more than the maximum number of allowed
acquisition attempts (30).
最后显示此错误
at java.lang.Thread.run(Unknown Source)
com.mchange.v2.resourcepool.ResourcePoolException: Unexpected Break Stack Trace!
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7d80f1f2 -- Unexpectedly Broken!!!
While trying to acquire a needed new resource, we failed
to succeed more than the maximum number of allowed
acquisition attempts (30).
at com.mchange.v2.resourcepool.BasicResourcePool.unexpectedBreak(BasicResourcePool.java:431)
at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:985)
at java.lang.Thread.run(Unknown Source)
com.mchange.v2.resourcepool.BasicResourcePool@c4767dd -- close() called multiple times.
com.mchange.v2.resourcepool.BasicResourcePool@c4767dd -- Unexpectedly broken!!!
com.mchange.v2.resourcepool.ResourcePoolException: Unexpected Break Stack Trace!
at com.mchange.v2.resourcepool.BasicResourcePool.unexpectedBreak(BasicResourcePool.java:431)
at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:985)
at java.lang.Thread.run(Unknown Source)
com.mchange.v2.resourcepool.BasicResourcePool@c4767dd -- close() called multiple times.
请告诉我代码中的问题是什么。我刚刚将整个代码移到了c3p0。这就是我犯了错误。谢谢你提前!!!
答案 0 :(得分:2)
您可以执行以下操作:
明确设置连接池的最大大小:
conn.setMaxPoolSize(50);
检查您是否正在关闭连接
使用
杀死未使用的(老化)连接来自文档
默认情况下,池永远不会过期。如果你希望 连接将随着时间的推移而过期,以保持“新鲜度”, 设置maxIdleTime和/或maxConnectionAge。 maxIdleTime定义了多少 秒应该允许连接在被使用之前不使用 从游泳池中剔除。 maxConnectionAge强制池剔除任何 从数据库获取的连接多于集合 过去的秒数。
文档: