I've been learning about connection pools, and I have gotten one working by using Tomcat's PoolProperties. Now I would like to set one up using a context.xml file with Tomcat and IntelliJ, but I can't get this to work. How is this done?
A new project with a 4.0 Web Application framework can automatically create a web/WEB-INF directory with a web.xml file, but the web/META-INF directory containing a context.xml file is not created. If I do create web/META-INF/context.xml myself through the Tool Window > Project, and I use this to set up a connection pool as shown below, the file seems to be ignored.
This is my guess as to what might be happening, though I'm sure this is filled with errors: A WAR file is a packaged directory that is sent to Tomcat in an arrangement that Tomcat understands. web/WEB-INF/web.xml is a required file that is needed for a variety of reasons, one of which is to set up servlets. web/META-INF/context.xml is optional, and thus by default, IntelliJ does not create it. When a web/META-INF/context.xml is created manually, it must also be included into the WAR file otherwise IntelliJ will not send it to Tomcat.
Even if the previous paragraph is true though, I still have not gotten Tomcat to use context.xml after trying to add it to the WAR. Maybe I didn't add it correctly.
<?xml version="1.0" encoding="UTF-8" ?>
<Context>
<Resource name="jdbc/sql_connection_03" auth="Container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/follow_db_01"
username="root" password="pass"
maxactive="100" maxIdle="30" maxWait="10000"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="java.sql.DataSource"/>
</Context>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<sql:query var="rs" dataSource="jdbc/sql_connection_03">
select ID, Description, PackageSize from products;
</sql:query>
<html>
<head>
<title>Index</title>
</head>
<body>
<c:forEach var="row" items="${rs.rows}">
${row.ID}<br>
${row.Description}<br>
${row.PackageSize}<br>
</c:forEach>
</body>
</html>
I had the same issue and adding context lookup to my java code to use the resource defined in context.xml did the job.
It's the accepted solution from: Tomcat and JDBC connection pooling
Still works with Tomcat 9.0.39
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With