挺好使用的JDBC连接池


placeholder image
admin 发布于:2009-12-18 22:57:00
阅读:loading

在spring框架中,有好几款连接池,加上Struts的连接池(在struts-config.xml文件中),网上多数说的是spring的dbcp连接池(现在看看你的spring项目中,有没有这个class="org.apache.commons.dbcp.BasicDataSource"啊)这些连接池一般都是加上框架的配置文件,再者就是更改tomcat配置文件的连接池,这些都不说我看中的,一般的改来改去给实施人员带来麻烦,有些东西是可以从程序上来避免的,但是前提是没有不避免的麻烦。行了,不废话了,这个连接池,我测试了1下,再1个页面用循环来循环打开、关闭30次感觉是非常的快,循环100次、1000次、3000次也并不是很影响效率,没有循环比3000大的数字了,没这么变态的人吧。循环3000次页面首次执行(服务器刚刚启动,用了7秒),刷新几次页面看到的时间都是需要4秒种左右。


测试代码如下:

image.png

不服气你可以在循环里面调用最普通原始的连接数据库关闭的方法,试试看,那个循环不了几千次内存就已经益处了。


调用连接池类:

public class ConnectionSource {

    private static BasicDataSource dataSource = null;

    /**

     */

    public ConnectionSource() {

    }

    /**

     * @param properties

     * @return

     */

    public static Properties analyseDBfile() {

        Properties properties = new Properties();

        InputStream is = ConnectionSource.class

                .getResourceAsStream("/db.properties");

        try {

            properties.load(is);

        } catch (IOException e) {

            e.printStackTrace();

        }

        try {

            is.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return properties;

    }

    /**

     */

    public static void init() {

        if (dataSource != null) {

            try {

                dataSource.close();

            } catch (Exception e) {

                //

            }

            dataSource = null;

        }

        try {

            Properties properties = analyseDBfile();

            dataSource = (BasicDataSource) BasicDataSourceFactory

                    .createDataSource (properties);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**

     * 获取连接

     *

     * @return

     */

    public static synchronized Connection getConnection() {

        if (dataSource == null) {

            init();

        }

        Connection conn = null;

        if (dataSource != null) {

            try {

                conn = dataSource.getConnection();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        return conn;

    }

    /**

     * @param args

     */

    public static void main(String[] args) {

        System.out.println(getConnection());

    }

}

数据库配置文件如下:

#driverName

driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver

url=jdbc:microsoft:sqlserver://localhost:1433;

username=sa

password=

maxActive=30

maxIdle=10

maxWait=1000

removeAbandoned=false

removeAbandonedTimeout=120

testOnBorrow=true

logAbandoned=true

说明:改连接池依赖与Apache提供的Commons里面的3个jar文件,分别是:

commons-pool-1.3.jar

commons-dbcp-1.2.2.jar

commons-collections-3.2.jar

在JDBC应用中,这里提供了获取连接的方法,至于关闭连接的方式,由于这个池里面实现了Connection接口,所有直接掉用connection.close();方法就哦了。

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:hhttps://www.chendd.cn/information/viewInformation/other/38.a

最后更新:2009-12-18 22:57:00

访问次数:253

评论次数:0

点赞个数:0

 点赞


 发表评论

当前回复:作者

 评论列表


留言区