我有这个类,但我不确定如何正确关闭连接,因为即使我只有 3 个用户登录但有多个 sql 查询,我仍然遇到此错误。
> com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
> Data source rejected establishment of connection, message from
> server: "Too many connections"
import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
public class UserDaoImpl implements UserDao
{
DataSource dataSource;
public DataSource getDataSource()
{
return this.dataSource;
}
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
public boolean isValidUser(String username, String password) throws SQLException
{
PreparedStatement pstmt = null;
ResultSet resultSet = null;
boolean rt = false;
try{
PasswordEncryptor pws = new PasswordEncryptor();
String encryptedPass = pws.encrypt(password);
String query = "Select count(1) from userdetails where username = ? and password = ?";
pstmt = dataSource.getConnection().prepareStatement(query);
pstmt.setString(1, username);
pstmt.setString(2, encryptedPass);
resultSet = pstmt.executeQuery();
if (resultSet.next()){
rt = (resultSet.getInt(1) > 0);
}
else{
rt = false;
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
resultSet.close();
pstmt.close();
dataSource.getConnection().close();
}
return rt;
}
}
Spring配置.xml
<bean name="userDao" class="com.spring.acadconnect.services.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/acadconnect" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
请注意您正在致电.getConnection()
多次。尽管文档在这方面可能更清楚DataSource.getConnection()实际上opens一个新的连接(而不是返回现有的连接),因此您需要关闭从该方法返回的每个实例。
As .getConnection()
每次调用此行时都会创建一个新实例,这是连接泄漏,因为它没有关闭返回的连接:
pstmt = dataSource.getConnection().prepareStatement(query);
此行浪费地打开一个新连接,但又立即关闭它:
dataSource.getConnection().close();
看起来您正在尝试为每次调用打开和关闭单独的连接isValidUser()
(因为您要在该方法调用结束时关闭连接)。即使您修复了上述泄漏,这也不是连接的用途。相反,您应该在应用程序中共享一个连接(或其中一小部分)。因此,当您的程序启动时,您打开这样一个连接,并且一旦整个节目不再需要连接(通常在终止前不久),您可以将其关闭。
这种行为通常由依赖注入,您可以在其中构建连接和其他资源,然后将它们传递到需要它们的任何对象中 - 这将资源管理与使用这些资源的代码分离。举个简单的例子:
public static void main(String[] args) {
DataSource dataSource = createDataSource();
try (Connection connection = dataSource.getConnection()) {
runProgram(connection);
}
}
/**
* this method doesn't need to worry about closing the Connection,
* it trusts that its caller will be responsible for that.
*/
private static void runProgram(Connection connection) {
// ...
}
根据经验,对象应该只负责关闭它们构造的对象,并且应该避免关闭它们传递的对象。在您当前的代码中UserDaoImpl
正在打开连接,所以它应该负责关闭它,但我建议传递Connection
反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)