I am using the Tomcat server in java and want to have access to the ServletContext from the restlet resource in order to access my cached DataSource object (for joining mysql connections). org.restlet.resource.Resource comes with a Context object, but it has nothing to do with ServletContext. Therefore, after some searches, I found the following:
final String contextKey = "org.restlet.ext.servlet.ServletContext"; final String poolKey = "MyCachedDBPool"; final Map<String, Object> attrs = getContext().getAttributes(); final ServletContext ctx = (ServletContext) attrs.get(contextKey); if (ctx == null) { throw new Exception("Cannot find ServletContext: " + contextKey); } final DataSource ds = (DataSource) ctx.getAttribute(poolKey); if (ds == null) { throw new DetourQAException("DataSource not stored in context" + poolKey + "attr"); }
But it returns null for ServletContext. Has anyone successfully accessed the ServletContext from a resident resource and how did you do it?
If this is not the recommended way to pool pools, then what is the best way to make a connection pool restart?
source share