W
WorldCreater
Unregistered / Unconfirmed
GUEST, unregistred user!
输出:
.get connection
========== after insert ==========
test
.get connection
========== after update ==========
try
.get connection
========== after delete ==========
close connection
连接只释放了一次
如果这样的话,数据库连接池内的连接不是很快就会被用光了吗?
怎么做才对?
===========================================
package com.bcstnet.struts.services;
import com.bcstnet.struts.services.ConnectionPool;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class DatabaseOperation {
private Connection connection = null;
public DatabaseOperation () {
init ();
}
public void init () {
System.out.println ("get connection");
connection = ConnectionPool.getConnection();
}
public void insert (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public void update (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public void delete (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public ResultSet query (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return statement.executeQuery (sql);
}
protected void finalize () throws Exception {
connection.close();
System.out.println ("close connection");
}
}
==============================================================
package com.bcstnet.struts.services.AllJUnitTests;
import junit.framework.TestCase;
import com.bcstnet.struts.services.DatabaseOperation;
import java.sql.ResultSet;
public class DatabaseOperationTest extends TestCase {
public static void main (String[] args) {
junit.textui.TestRunner.run (DatabaseOperationTest.class);
}
public void testInsert () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("insert into T_Member(LoginName,LoginPassword)");
stringBuffer.append (" values('test','test')");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
System.out.println ("========== after insert ==========");
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
public void testUpdate () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("update T_Member set LoginName='try'");
stringBuffer.append (" where loginName='test'");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
System.out.println ("========== after update ==========");
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
public void testDelete () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("delete T_Member ");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
System.out.println ("========== after delete ==========");
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
}
.get connection
========== after insert ==========
test
.get connection
========== after update ==========
try
.get connection
========== after delete ==========
close connection
连接只释放了一次
如果这样的话,数据库连接池内的连接不是很快就会被用光了吗?
怎么做才对?
===========================================
package com.bcstnet.struts.services;
import com.bcstnet.struts.services.ConnectionPool;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class DatabaseOperation {
private Connection connection = null;
public DatabaseOperation () {
init ();
}
public void init () {
System.out.println ("get connection");
connection = ConnectionPool.getConnection();
}
public void insert (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public void update (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public void delete (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
statement.executeUpdate (sql);
}
public ResultSet query (String sql) throws Exception {
Statement statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return statement.executeQuery (sql);
}
protected void finalize () throws Exception {
connection.close();
System.out.println ("close connection");
}
}
==============================================================
package com.bcstnet.struts.services.AllJUnitTests;
import junit.framework.TestCase;
import com.bcstnet.struts.services.DatabaseOperation;
import java.sql.ResultSet;
public class DatabaseOperationTest extends TestCase {
public static void main (String[] args) {
junit.textui.TestRunner.run (DatabaseOperationTest.class);
}
public void testInsert () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("insert into T_Member(LoginName,LoginPassword)");
stringBuffer.append (" values('test','test')");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
System.out.println ("========== after insert ==========");
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
public void testUpdate () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("update T_Member set LoginName='try'");
stringBuffer.append (" where loginName='test'");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
System.out.println ("========== after update ==========");
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
public void testDelete () throws Exception {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append ("delete T_Member ");
DatabaseOperation databaseOperation = new DatabaseOperation ();
databaseOperation.insert (stringBuffer.toString());
stringBuffer = new StringBuffer ();
stringBuffer.append ("select * from T_Member");
ResultSet resultSet = databaseOperation.query (stringBuffer.toString());
System.out.println ("========== after delete ==========");
while (resultSet.next()) {
System.out.println (resultSet.getString("loginName"));
}
}
}