How to register connection pool in tomcat with JSP?

Viewed 84

I want to connect connection-pool(DBCP?) to mysql-db and register in tomcat.

So, I install some of *.jar files(DHCP, Pool, Collection) into "C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib".

Then, I execute my code below, but, it doesn't work.

<%@page contentType="text/html;charset=utf-8"%>
<%@page import="org.apache.commons.dbcp2.*"%>
<%@page import="org.apache.commons.pool2.impl.*"%>

<%
    GenericObjectPool objectPool = new GenericObjectPool();
    DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
        "jdbc:mysql://localhost:3306/webdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
        "root", "****"
    );
    new PoolableConnectionFactory(connectionFactory, objectPool);

    PoolingDriver driver = new PoolingDriver();
    driver.registerPool("/webdb_pool", objectPool);
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>Pool is registered!!!</h3>
</body>
</html>

And the errors are:

The constructor GenericObjectPool() is undefined

The constructor PoolableConnectionFactory(DriverManagerConnectionFactory, GenericObjectPool) is undefined

enter image description here

how to fix them..??

1 Answers

Let's try:

GenericObjectPool connectionPool = new GenericObjectPool(null);
DriverManagerConnectionFactory connectionFactory = 
     new DriverManagerConnectionFactory(URL, username, password);
PoolableConnectionFactory poolableConnectionFactory =
     new PoolableConnectionFactory(connectionFactory, connectionPool, null, 
         null, false, false);
poolingDataSource = new PoolingDataSource(connectionPool);

Related