Closed connections are still in the connection pool - why?
servlet -
public class Index extends HttpServlet { TimeZoneService timeZoneService; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException { WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); timeZoneService = (TimeZoneService) ctx.getBean("timeZoneService"); timeZoneService.loadAllTimeZones(); System.out.println("Done"); } } public interface TimeZoneService { void loadAllTimeZones(); } public class TimeZoneServiceImpl implements TimeZoneService { private TimeZoneDao tzDao; private Map<Long, String> tzOid2JavaName = new HashMap<Long, String>(); public void loadAllTimeZones() { List<TimeZone> timeZones = tzDao.findAllTimeZones(); for (TimeZone tz : timeZones) { tzOid2JavaName.put(tz.getOid(), tz.getJavaName()); } } public void setTzDao(TimeZoneDao tzDao) { this.tzDao = tzDao; } } public interface TimeZoneDao { List<TimeZone> findAllTimeZones() throws DataAccessException; } public class TimeZoneDaoImpl extends JdbcDaoSupport implements TimeZoneDao { public List<TimeZone> findAllTimeZones() throws DataAccessException { StringBuffer sql = new StringBuffer(); sql.append("SELECT TZ.OID, TZ.JAVA_NAME FROM TIME_ZONE TZ"); List<TimeZone> timeZones = getJdbcTemplate().query(sql.toString(), new RowMapper() { public Object mapRow(ResultSet rs, int i) throws SQLException { TimeZone tz = new TimeZone(); tz.setOid(rs.getLong("OID")); tz.setJavaName(rs.getString("JAVA_NAME")); return tz; } }); return timeZones; } } public class TimeZone { private Long oid; private String javaName; public Long getOid() { return this.oid; } public void setOid(Long oid) { this.oid = oid; } public String getJavaName() { return this.javaName; } public void setJavaName(String javaName) { this.javaName = javaName; } }
spring -config.xml
<beans> <jee:jndi-lookup id="dataSource" jndi-name="java:/OracleDS"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="timeZoneDao" class="dao.impl.TimeZoneDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="timeZoneService" class="logic.impl.TimeZoneServiceImpl"> <property name="tzDao" ref="timeZoneDao"/> </bean> </beans>
web.xml
<web-app> <display-name>Spring</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/spring-config.xml,classpath*:/META-INF/spring-config.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>index</servlet-name> <display-name>Index page</display-name> <description>Landing page</description> <servlet-class>servlet.Index</servlet-class> </servlet> <servlet-mapping> <servlet-name>index</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> <!-- Session Timeout (in minutes) --> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
MySQL-ds.xml
<datasources> <local-tx-datasource> <jndi-name>OracleDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/spring</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>spring_test</user-name> <password>spring_test13</password> <min-pool-size>1</min-pool-size> <max-pool-size>5</max-pool-size> <idle-timeout-minutes>2</idle-timeout-minutes> </local-tx-datasource> </datasources>
source share