Java EE web project on servlets. Tomcat 9 is used as a container. You need to write a ConnectionPool .
At the root of the project in the META-INF folder is the context.xml :
<Context> <Resource name="list" type="javax.sql.DataSource" maxTotal="12" maxIdle="8" maxWaitMillis="10000" username="abc" password="abc" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/list" /> </Context> At the root of the project in WEB-INF in web.xml :
<resource-ref> <description>MySQL DB Connection Pool</description> <res-ref-name>list</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> The connection pool itself:
public class ConnectionPool { private static DataSource dataSource; private ConnectionPool() { } static { try { InitialContext initContext= new InitialContext(); dataSource = (DataSource) initContext.lookup("java:comp/env/list"); } catch (NamingException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection connection = dataSource.getConnection(); return connection; } } The application crashes on the Connection creation line:
Connection connection = dataSource.getConnection(); update: brought the name to one form (in context.xml, in web.xml in ConnectionPool)
Now instead of NullPointer I catch this:
javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) How to live?