社区
Web 开发
帖子详情
求一正确的操作连接池的类
Building
2005-12-19 10:41:33
哪位大侠能提供一个比较好的操作连接池的类?考虑到资源释放等因素。
...全文
150
4
打赏
收藏
求一正确的操作连接池的类
哪位大侠能提供一个比较好的操作连接池的类?考虑到资源释放等因素。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
skycncomp
2005-12-19
打赏
举报
回复
瞎写的,不知道对不对。
DataSource ds = null;
Connection conn = null;
Statement stat = null;
ResultSet rst = null;
try {
DataSource ds = ctx.lookup("name");
conn = ds.getConnection();
//这里操作statement
rst.close();
statement.close();
rst.close();
} catch (SQLException e) {
try {
rst.close();
rst = null;
} finally {
if (null != rst) {
rst.close();
}
}
try {
stat.close();
stat = null;
} finally {
if (null != stat) {
stat.close();
}
}
try {
conn.close();
conn = null;
} finally {
if (null != rst) {
conn.close();
}
}
}
接分
beihehuaiyu
2005-12-19
打赏
举报
回复
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class CreateDBO {
private static InitialContext initialcontext = null;
private static DataSource datasource = null;
public CreateDBO() {
}
public static Connection getCenterConnection(boolean autocommit) {
Connection connection = null;
try {
if (initialcontext == null)
initialcontext = new InitialContext();
if (datasource == null) {
/**
* 相关参数在.xml文件里面设置
*/
datasource = (DataSource) initialcontext
.lookup("java:/datasource");
if (datasource == null)
datasource = (DataSource) initialcontext
.lookup("java:comp/env/jdbc/datacenter");
}
connection = datasource.getConnection();
connection.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static Connection getCenterConnection() {
Connection connection = getCenterConnection(false);
return connection;
}
public static PreparedStatement getPreparedStatement(Connection conn,
String StatementString) {
PreparedStatement preparedstatement = null;
try {
preparedstatement = conn.prepareStatement(StatementString);
} catch (Exception e) {
e.printStackTrace();
}
return preparedstatement;
}
public static Statement getStatement(Connection conn) {
Statement statement = null;
try {
statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return statement;
}
public void Commit(Connection conn) {
try {
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Rollback(Connection conn) {
try {
conn.rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
public void CloseConnection(Connection connection, boolean isSuccess) {
try {
// Rollback or commit the transaction
try {
if (isSuccess == false) {
connection.rollback();
} else {
if (connection != null) {
if (!connection.getAutoCommit()) {
try {
connection.commit();
} catch (Exception e) {
connection.rollback();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void CloseConnection(Connection connection) {
try {
// commit the transaction
try {
if (connection != null) {
if (!connection.getAutoCommit()) {
try {
connection.commit();
} catch (Exception e) {
connection.rollback();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void CloseStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void CloseResultSet(ResultSet rst) {
try {
if (rst != null) {
rst.close();
rst = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
skycncomp
2005-12-19
打赏
举报
回复
^*_*^你啥意思?
Building
2005-12-19
打赏
举报
回复
楼上的可能没有理解我的意思
Java源码系列-手写数据库
连接池
(附源码)
为了理解数据库
连接池
的底层原理,我们可以自己手写一个
类
似Hikari,Druid一样的高性能的数据库
连接池
!通过手写数据库
连接池
掌握数据库
连接池
底层运行原理,胜任企业级开发、提高编程内功!!! 数据库
连接池
在...
Java数据库
连接池
详解
数据库
连接池
是系统初始化过程中创建一定数量的数据库连接放于
连接池
中,当程序需要访问数据库时,不再建立一个新的连接,而是从
连接池
中取出一个已建立的空闲连接,使用完毕后,程序将连接归还到
连接池
中,供其他...
Java使用数据库
连接池
连接Oracle数据库
在这种情况下,出现了使用数据库
连接池
的方法,
类
似于线程池,初期创建一定数量的连接供应用程序使用,当使用完成后将其归还给
连接池
而不是销毁,这样有效的提高了资源利用率,下面分享一种简单的创建
连接池
的方法:...
C3P0
连接池
的基本配置与使用
上一期我写了一篇Druid
连接池
的基本配置与使用,今天我要介绍一下C3P0
连接池
的基本使用。因为是介绍基本使用,所以我打算用一个非常简单的java应用来教大家如何对C3P0
连接池
进行基本配和使用。所以你只要有jdbc和...
mysql
连接池
多少合适_数据库
连接池
大小设置为多少更合适
数据库连接是有限的、昂贵的资源,一个数据库连接对象对应一个物理数据库的连接,如果每次数据库
操作
都创建新的连接,使用完后释放,会导致系统性能低下,这就引出了
连接池
的概念。
连接池
的必要性数据库
连接池
是负责...
Web 开发
81,091
社区成员
341,719
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章