I am encountering multiple memory leaks in my multi-module Maven project, and I am unable to identify the root cause despite extensive debugging. I have tried using tools like SonarQube, PMD, CheckStyle, and SpotBugs, but nothing has resolved the issue.
Here is an overview of my environment:
- Java version: 17
- Tomcat version: 10
- JNDI data source: Managed by Tomcat
- MySQL driver: mysql-connector-j-9.1.0 (available only in Tomcat's lib folder, not in Maven POM files)
- Spring framework version: 6.2.0
- Spring Security version: 6.2.3
- Hibernate version: 6.2.0.Final
- Hibernate Commons: 7.0.1.Final
Here is the relevant configuration:
HikariCP data source configuration:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="dataSourceJNDI" value="java:/comp/env/jdbc/xxx" />
<property name="maximumPoolSize" value="10" />
<property name="minimumIdle" value="10" />
<property name="idleTimeout" value="300000" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="leakDetectionThreshold" value="30000" />
<property name="autoCommit" value="true" />
</bean>
EntityManager configuration:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="xxxxx" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<entry key="hibernate.enable_lazy_load_no_trans" value="true" />
<entry key="hibernate.jdbc.batch_size" value="50" />
</map>
</property>
</bean>
JNDI resource configuration in Tomcat:
<Resource name="jdbc/MainDB" auth="Container"
type="javax.sql.DataSource" username="rrrrr" password="ttttt"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://xxxxx/yyyyy?useSSL=false&dontTrackOpenResources=true"
maxIdle="4" maxTotal="2000" minIdle="2"
testOnBorrow="true" validationQuery="SELECT 1"
removeAbandonedOnBorrow="true" removeAbandonedTimeout="600"
logAbandoned="true" />
Abstract DAO implementation (GenericDaoImpl):
@PersistenceContext
protected EntityManager entityManager;
I am not directly managing sessions or connections, as everything is delegated to Tomcat, Hibernate, and Spring. Despite configuring HikariCP and using JNDI for connection pooling, I continue to see memory leak issues.
I suspect that the problem might be related to either:
- Tomcat's handling of the MySQL driver.
- Hibernate's lazy loading or batch processing configuration.
I have enabled Hibernate's generate_statistics and show_sql options but have not identified anything unusual.
Could someone help me troubleshoot this issue or provide guidance on common pitfalls that might cause memory leaks in a similar setup? Any tools, techniques, or configurations I might have overlooked would also be greatly appreciated.