Creating a database connection pool

前端 未结 7 2033
失恋的感觉
失恋的感觉 2020-12-30 12:48

Need information on creating a connection pool to the database (irrespective of the database) , and how efficient they are? What are the conditions where they can enhance pe

相关标签:
7条回答
  • 2020-12-30 13:21

    Have a look at BoneCP (http://jolbox.com) in the benchmark section for some numbers. Remember that preparedStatements etc are tied to a connection so you'll need to prepare them again and again if you're dealing with connections yourself (a connection pool will cache those for you too).

    My best solution so far: Use a lazyDataSource that only gives you a connection when you really need it (i.e. not blindly - if the data can come from a cache then you can avoid the database hit)

    0 讨论(0)
  • 2020-12-30 13:25

    Creating database connection pool using tomcat

    1. Tomcat enter resource inside : conf/context.xml

    Put the resource entries in context.xml file:

    <!-- jdbc/jndiName jndi --> 
    <Resource name="jdbc/jndiName" auth="Container" type="javax.sql.DataSource" initialSize="1" maxActive="100" maxIdle="30" maxWait="10000" username="enter username" password="enter password" driverClassName="diver name" url="jdbc database url"/>
    

    2. create a class which will create the connection pool

    public class MyConnectionFactory {
        private static String module = "[ QuoteConnectionFactory ]";
        private static QuoteConnectionFactory connectionFactory;
    
        protected QuoteConnectionFactory() {
        }
    
        /**
         *
         * @return=>getInstance() is a static method which will return the instance
         *                        of its own class
         */
        public static QuoteConnectionFactory getInstance() {
            if (connectionFactory == null)
                connectionFactory = new QuoteConnectionFactory();
            return connectionFactory;
        }
    
        /**
         *
         * @param jndiName
    
         */
        public Connection getConnection(String jndiName) {
            System.out.println("jndiName=======" + jndiName);
            Connection conn = null;
            InitialContext cxt = null;
            DataSource dataSource = null;
            try {
                cxt = new InitialContext();
                Context envContext  = (Context)cxt.lookup("java:/comp/env");
                dataSource = (DataSource)envContext.lookup(jndiName);
            } catch (NamingException e) {
    
            } catch (Exception e) {
    
            }
    
            if (dataSource == null) {
    
                try {
                    conn = dataSource.getConnection();
                } catch (Exception e) {
    
                }
    
                System.out.println("connection===================" + conn);
                return conn;
            }
        }
    

    3. edit web.xml file

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/jndiName</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    4. Use in code

    Connection con= QuoteConnectionFactory.getInstance(). getConnection("jndiName");
    
    0 讨论(0)
  • 2020-12-30 13:35

    Creating connections to databases are very expensive operations. Connection pools are instances of database connections that are created and cached. Anytime a new connection to a database is desired, one from the pool is used instead of creating a new connection. Some platforms like .NET + SQL Server use connection pools by default (you don't need to create your own). So, they basically enhance performance by saving time in creating new connections each time.

    0 讨论(0)
  • 2020-12-30 13:37

    Your question is a bit ambiguous:

    Do you want to homegrow a connection pool implementation? If so, this is a nice starting point: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html But this is highly discouraged for production environments. Better use an existing and thoroughly tested connection pooling API, like DBCP or C3P0.

    Or do you want to know how to use a connection pool? If so, the answer depends on the connection pooling API you're using. It's fortunately usually available at the website of the API in question.

    Or do you want to know when/why to use a connection pool? If so, it will surely enhance connecting performance if you have a long-living application (e.g. a webapplication) and you need to connect the database more than often. The normal JDBC practice is namely: acquire and close the Connection, Statement and ResultSet in the shortest possible scope (i.e. inside the very same method block). Because connecting is fairly expensive and can take up to 200ms of time or even more, using a connection pool is much faster. It gives connections on demand and takes care about actually closing the connection. That does however not mean that you may change the way you write JDBC, you still need to acquire and close them in the shorest possible scope. The only thing you need to change is the way you acquire the connection. E.g. change from

    connection = driverManager.getConnection();
    

    to

    connection = connectionPool.getConnection();
    

    No more changes are needed as long as your JDBC code is well-written.

    0 讨论(0)
  • 2020-12-30 13:37

    Using a connection pool, you save time at every access because connection is already established.

    Moreover, at least on Oracle, you keep the compiled statement linked to the connection, so repetitive execution of same SQL statement is even quicker.

    (see PreparedStatement if you are in Java/JDBC)

    The only risk of counter-performance is when you keep too many idle connections in your pool, the associated ressources (your side and on database) are wasted.

    0 讨论(0)
  • 2020-12-30 13:40

    Creating a database connection may or may not be an expensive operation, depending on your environment and what you intend to do with it.

    If you're going to run a single very easy query, then connecting probably takes as long (or longer) than the query.

    Some databases have a much bigger connection overhead than others; if tuned correctly, mysql should have very little (above the time to make a tcp connection and do the protocol handshake). However, if latency to your server is very high, even this can be quite significant (particularly if you intend to do only a few queries).

    If you're planning to do, say, 100 queries, or a few really slow queries, then the connection time disappears into insignificance.

    In generally I'd say open a new connection each time until you can demonstrate that it's a real performance problem. Using connection pooling can lead to BUGS, which we don't like:

    • Connection state wasn't COMPLETELY reset after the previous use in the pool - so some state lingers and creates unexpected behaviour resulting in a bug
    • Connection was closed in some way (perhaps by a stateful firewall timeout) which cannot be detected, therefore an app tries to use a closed connection, causing a long delay or failure
    0 讨论(0)
提交回复
热议问题