簡體   English   中英

Tomcat連接池方法

[英]Tomcat connection pooling method

我正在開發一個J2EE應用程序,它將接收來自移動應用程序的請求,並執行一些數據庫操作以向用戶顯示數據。

所以數據庫連接池對我來說非常重要。我在我的J2EE應用程序的META-INF文件夾中聲明了context.xml 。它看起來如下

<Context debug="0"
    reloadable="true" crossContext="false" privileged="true" cookies="true" >

     <Resource name="jdbc/servicedb" 
               auth="Container"
               type="javax.sql.DataSource" 
               driverClassName="oracle.jdbc.OracleDriver"
               username="XXXXXX"
               password="XXXXXX"
               url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX"
               initialSize="10"
               maxActive="100"
               maxIdle="50"
               minIdle="10"
               suspectTimeout="60"
               timeBetweenEvictionRunsMillis="30000"
               minEvictableIdleTimeMillis="60000"/>
</Context>

這是我的連接類中返回連接的方法

public static Connection getConnection()
    {
        Connection dbConnection = null;

        //loading from the dao.properties file
        DAOProperties properties = new DAOProperties("com.jndi");
        String url = properties.getProperty(PROPERTY_URL, true);
        String driverClassName = properties.getProperty(PROPERTY_DRIVER, false);
        String password = properties.getProperty(PROPERTY_PASSWORD, false);
        String username = properties.getProperty(PROPERTY_USERNAME, password != null);

     // If driver is specified, then load it to let it register itself with DriverManager.
        if(driverClassName !=null){
            try
            {
                Class.forName(driverClassName);
                dbConnection = DriverManager.getConnection(url, username, password);

            }catch(ClassNotFoundException e){
                // Could not find the database driver
                e.printStackTrace();
                System.out.println("database driver error");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
     // Else assume URL as DataSource URL and lookup it in the JNDI.
        else{
            Context initContext;
            DataSource ds;
            try {
                initContext = new InitialContext();
                Context envContext  = (Context)initContext.lookup(JNDI_ROOT);
                if(envContext!=null){
                    ds = (DataSource)envContext.lookup(url);
                    if(ds!=null){
                        try {
                            dbConnection = ds.getConnection();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
        return dbConnection;
    }

DAOProperties是一個用於加載Properties文件的包裝類。我從這里獲取它

我正在關閉這樣的連接

public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                System.err.println("Closing Connection failed: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

最后我使用連接來填充這樣的列表

public loadlist(){
        Connection dbConnection = null;
        try{
            dbConnection = Connection.getConnection();
        if(dbConnection !=null){

            System.out.println("Connected to database");
            LOGGER.debug("Successfully connected to database");
        else{
                System.out.println("No Connection Exists");
                LOGGER.debug("unable to connect to database");
        }
        }catch (SQLException e) {
            // Could not connect to the database
            e.printStackTrace();
            System.out.println("database connection failed");
            LOGGER.debug("database connection failed due to SQLException");
        }finally{
            Connection.close(dbConnection);
            LOGGER.debug("connection closed");
        }
        return result;
    }

剛才我已經在連接池上完成了這個

我懷疑我是否可以關閉連接或將其返回池中?

我還想知道如何返回到池的連接?

請幫忙 。

假設您的代碼在getConnection()方法中使用else路徑(即它執行JNDI查找而不是使用DriverManager ),您對連接池的使用是正確的。

您無法在關閉連接並將其返回池中進行選擇。 如果從池中獲得連接,則close()會將其返回到池中(而不是關閉它)。 別無選擇。 您只能關閉自己創建的連接。

我不明白你為什么要編寫自己的連接池。

我建議廢棄這種方法並使用由Tomcat管理的JNDI數據源和其他人編寫的池。 Apache有一個很好的DBCP - 為什么要寫自己的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM