开发者

Tomcat connection pooling method

开发者 https://www.devze.com 2023-04-12 23:40 出处:网络
I am developing a J2EE application which will receive requests from a mobile application and does some database operations to show the data to the user.

I am developing a J2EE application which will receive requests from a mobile application and does some database operations to show the data to the user.

So database connection pooling is of great importance to me.I declared context.xml in my META-INF folder of my J2EE application.It looks as follows

<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>

And this is a method from my connection class which returns a connection

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 is a wrapper class for loading the Properties file.I took it from here

I am closing the connection like this

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();
            }
        }
    }

And finally 开发者_StackOverflow中文版i am using the connection to polulate a list like this

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;
    }

Just now i have gone through this on connection pooling.

I have an doubt whether i can close my connection or return it to the pool?

I also want to know how to return a connection to the pool?

Please help .


Assuming that your code takes the else path in the getConnection() method (i.e. it does a JNDI lookup instead of using the DriverManager), your use of the connection pool is correct.

You cannot choose between closing a connection and returning it to the pool. If you got the connection from the pool, close() will return it to the pool (instead of closing it). There's no other option. You can only close connection that you have created yourself.


I don't see why you'd write your own connection pool.

I'd recommend scrapping this approach and going with JNDI data sources, managed by Tomcat, and a pool written by someone else. Apache has a good DBCP - why write your own?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号