Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Connection Pooling

Empfohlene Antworten

Veröffentlicht

Hallo ihr!

ich hab da mal ne frage. Hat einer von euch schon mal was wegen Connection Pooling unter Java JDBC zu tun gehabt und kann mir da ein wenig weiterhelfen!

Ich möchte eine KLasse entwickeln die SQL Connections verwaltet und je nach anfrage an die Klasse soll dann eine Connection genutzt werden oder erst geöffnet und gesichert werden und dann das Kommando abgesetzt werden.

Servus,

jeder ApplicationServer nutzt ConnectionPooling für JDBC Connections. Du könntest in deren Sourcen schauen (www.jboss.org).

Im Prinzip ist es aber ganz einfach: Deine Klasse kann Connections halten (in irgendeiner Collection). Deine Klasse weiss, wieviele Connections sie halten darf (über eine Variable mitgeteilt). Deine Klasse verfügt über eine Methode, die eine Connection zurückliefert. Wenn diese Methode aufgerufen wird, schaust Du nach, ob Du bereits eine Connection übrig hast, die Du noch nicht weggegeben hast (das musst Du Dir auch irgendwo merken) oder ob Du eine erstellen musst.

Fertig.

Peter

Beispiel, ist schon etwas älter (2 Jahre), sollte aber seinen Zweck erfüllen...


package de.emediaoffice.elk.database;


//------------------------------------------------------------------

//--- Import

//------------------------------------------------------------------

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Enumeration;

import java.util.Hashtable;



/**

******************************************************************

*** Module: CElkConnectionPool

*** Description: Connection pooling class for elk database

***              connections

******************************************************************

*** Copyright 2000, 2001 by Technical Office GmbH

*** Copyright 2002 by EMedia Office GmbH

******************************************************************

*** @Author Timo Haberkern

*** Created at: 17.08.2001

******************************************************************

*** $Version: V.1.2.0 Build 317 $

*** $Date: Mittwoch, 19. November 2003 16:31:00 $ $Revision: 1.24 $

******************************************************************

*/

public class CElkConnectionPool extends Object

{

    private Hashtable m_hashConnections;

    private int m_nIncrement;

    private String m_strDbURL;

    private String m_strDbUser;

    private String m_strDbPassword;


    /**

    ****************************************************************

    *** Constructor for initializing the connection pool

    ****************************************************************

    *** @param The JDBC URL of the database

    *** @param The user-name for the database

    *** @param The password for the database

    *** @param The name of the jdbc driver that is used for the pool

    *** @param The number of initial connections that are created

    *** @param The The number of new connections if the initial

    ***        size is not enough

    ****************************************************************

    */

    public CElkConnectionPool(String dbURL, String user, String password, String driverClassName,

                               int initialConnections, int increment) throws SQLException, ClassNotFoundException

    {

        Class.forName(driverClassName);

        m_strDbURL = dbURL;

        m_strDbUser = user;

        m_strDbPassword = password;

        m_nIncrement = increment;


        m_hashConnections = new Hashtable();


        //System.out.println("Database:"+dbURL+","+user+","+password+","+increment);

        for(int nIndex=0; nIndex < initialConnections; nIndex++)

        {

            m_hashConnections.put(DriverManager.getConnection(m_strDbURL, m_strDbUser, m_strDbPassword), Boolean.FALSE);

        }

    }


    /**

    ****************************************************************

    *** Delivers a connection from the pool

    ****************************************************************

    *** @return The connection from the pool

    ****************************************************************

    */

    public Connection getConnection() throws SQLException

    {

        Connection curConnection = null;


        Enumeration conKeys = m_hashConnections.keys();


        synchronized (m_hashConnections)

        {

            while(conKeys.hasMoreElements())

            {

                curConnection = (Connection)conKeys.nextElement();


                Boolean b = (Boolean)m_hashConnections.get(curConnection);


                if(b==Boolean.FALSE)

                {

                    try

                    {

                        curConnection.setAutoCommit(true);

                    }

                    catch(SQLException e)

                    {

                        curConnection=DriverManager.getConnection(m_strDbURL, m_strDbUser, m_strDbPassword);

                    }


                    m_hashConnections.put(curConnection, Boolean.TRUE);

                    return curConnection;

                }

            }

        }


        for(int nIndex = 0; nIndex < m_nIncrement; nIndex++)

        {

            m_hashConnections.put(DriverManager.getConnection(m_strDbURL, m_strDbUser, m_strDbPassword), Boolean.FALSE);

        }


        return getConnection();

    }


    /**

    ****************************************************************

    *** Puts a connection back to the pool

    ****************************************************************

    *** @param The connection that is returned to the pool

    ****************************************************************

    */

    public void returnConnection(Connection returned)

    {

        Connection curConnection;


        Enumeration conKeys = m_hashConnections.keys();

        while(conKeys.hasMoreElements())

        {

            curConnection = (Connection)conKeys.nextElement();

            if(curConnection == returned)

            {

                m_hashConnections.put(curConnection, Boolean.FALSE);

                break;

            }

        }

    }



    public void releaseAllConnections()

    {

        Connection curConnection = null;


        Enumeration conKeys = m_hashConnections.keys();


        synchronized (m_hashConnections)

        {

            while(conKeys.hasMoreElements())

            {

                curConnection = (Connection)conKeys.nextElement();

                try

                {

                    if (curConnection.isClosed() == false)

                        curConnection.close();

                }

                catch (Exception ignored)

                {

                }

            }

        }

    }

}

schau bir mal die commons-dbcp von jankata an

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.