我有一个需要优化的旧应用程序,其中我的方法返回java.sql.ResultSet
,而且我具有单个连接,所以现在我需要实现连接池,现在的问题是如果我关闭连接,则结果集在执行时会给出异常rs.next()
所以我想要一个类似于ResultSet的类,可以说它与Connection分离,这样我就不必在使用ResultSet
对象的JSP上进行更改。
Here's what I would recommend:
Write your persistence methods so you pass the connection in. Let another object (eg a service) manage getting the connection out of the pool, transactions, and cleanup.
Your persistence classes should create and close PreparedStatement
and ResultSet
objects within the method scope. Do not pass a ResultSet
around; map it into an object or data structure of some kind and return that to the caller.
Your JSPs should not be dealing with ResultSets
. They should be iterating over the object or data structure that you mapped the ResultSet
into when you called the persistence tier.
Let me guess: your JSPs are filled with scriptlets. You must throw that away, too. Time to learn JSTL.
Read about Model-2 MVC.
Got the solution I had used CachedRowSet
package taher.connection;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import org.apache.commons.dbcp.BasicDataSource;
/**
*
* @author taher_JAVAHUNTER
*/
public class DataTransaction {
private final static String username = "test";
private final static String password = "test";
private final static String url = "jdbc:mysql://127.0.0.1:3309/test";
public Connection connection = null;
public Statement statement = null;
ResultSet rs = null;
public static int connectionCount = 0;
public DataTransaction(boolean setCon) {
try {
setConnection();
} catch (Exception e) {
System.out.println("Error in Connection:" + e.toString());
}
}
public static BasicDataSource dataSource;
public void setConnection() throws SQLException {
try {
if (dataSource == null) {
dataSource = new BasicDataSource();
String driver = "com.mysql.jdbc.Driver";
try {
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxActive(100);
dataSource.setMaxWait(10000);
dataSource.setMaxIdle(10);
if (connection == null || connection.isClosed()) {
System.out.println(" requeition CONNECTION WITH FIRST SERVER.");
connection = dataSource.getConnection();
connectionCount++;
}
} catch (SQLException e) {
System.out.println("***Connection Requisition*** Could not connect to the database msg :" + e.getMessage());
}
} else {
System.out.println("NumActive : "+dataSource.getNumActive());
System.out.println("NumIdle : "+dataSource.getNumIdle());
System.out.println("NumTestsPerEvictionRun : "+dataSource.getNumTestsPerEvictionRun());
if (connection == null || connection.isClosed()) {
connection = dataSource.getConnection();
connectionCount++;
}
}
} catch (Exception e) {
System.out.println("open connection exception" + e);
}
}
public CachedRowSet viewQuery(String query) throws SQLException, Exception {
//query = query;
CachedRowSetImpl crs = new CachedRowSetImpl();
CachedRowSet crs2 = null;
try {
if (connection.isClosed()) {
setConnection();
}
System.out.println("Connection count 1 : " + connectionCount);
statement = connection.createStatement();
rs = statement.executeQuery(query);
crs.populate(rs);
crs2 = crs.createCopy();
closeConnection();
System.out.println("Connection count 2 : " + connectionCount);
} catch (Exception e) {
e.printStackTrace();
}
return crs2;
}
public void closeConnection() throws SQLException {
try {
if (statement != null) {
statement.close();
} else {
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
}
}
public static void main(String[] args) throws SQLException, Exception {
DataTransaction dt = new DataTransaction(true);
for (int i = 0; i < 10; i++) {
ResultSet rs = dt.viewQuery("select * from tbl_test");
//ResultSet rs = dt.viewQuery("select * from tbl_test",0);
System.out.println("Connection closed : " + dt.connection.isClosed());
while (rs.next()) {
System.out.println("testId : " + rs.getString(1));
System.out.println("testName : " + rs.getString(2));
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.