添加@ManyToOne/@OneToOne注解后部署应用触发JDBC驱动内存泄漏错误求解
Hey there, let's work through this JDBC driver memory leak issue you're hitting when using @ManyToOne or @OneToOne annotations in your RequisitionDetail model. I've dealt with similar headaches before, so here are actionable steps to fix it:
The error often pops up when JPA/Hibernate can't properly resolve entity associations, leading to repeated driver initialization attempts. Let's make sure your RequisitionDetail class is configured correctly:
@Entity public class RequisitionDetail { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Correct @ManyToOne with explicit join column and lazy fetch @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "requisition_id", referencedColumnName = "id", nullable = false) private Requisition requisition; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false) private Product product; private Integer quantity; private BigDecimal unitPrice; private BigDecimal totalPrice; // Add constructors, getters, and setters here }
- Use
FetchType.LAZYto avoid unnecessary immediate loading of associated entities (reduces driver interactions during deployment) - Always specify
@JoinColumnexplicitly to avoid JPA generating unexpected table/column names that cause resolution failures
This memory leak warning often stems from duplicate driver loading or incompatible versions:
- Check dependency scope: If you're using Maven/Gradle, ensure your JDBC driver (e.g., MySQL Connector) uses the correct scope. For most apps,
runtimeis ideal:<!-- Maven example for MySQL driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> <scope>runtime</scope> </dependency> - Avoid duplicate driver copies: Don't place the JDBC driver JAR in both your app's
WEB-INF/liband your application server's (e.g., Tomcat)libdirectory. Pick one location (preferably your app'sWEB-INF/libfor isolated deployments).
Tweak your persistence settings to reduce driver initialization overhead:
If you're using Spring Boot, add these properties to application.properties:
# Prevent unnecessary driver reloads spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true # Configure a reasonable connection pool size (adjust based on your needs) spring.datasource.hikari.maximum-pool-size=10 # Use appropriate DDL mode (avoid create-drop in production) spring.jpa.hibernate.ddl-auto=update
For non-Spring Boot apps, add these properties to persistence.xml:
<property name="hibernate.connection.provider_disables_autocommit" value="true"/> <property name="hibernate.hikari.maximumPoolSize" value="10"/>
Tomcat's memory leak detector can trigger this warning even if the issue is minor. Fix the root cause first, but you can also tweak Tomcat's context.xml to reduce false positives:
<Context antiJARLocking="true" antiResourceLocking="true"> <!-- Your datasource configuration here --> <Resource name="jdbc/YourDatasource" auth="Container" type="javax.sql.DataSource" maxTotal="10" maxIdle="5" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/your_db" username="db_user" password="db_pass"/> </Context>
antiJARLockingandantiResourceLockingprevent Tomcat from locking JARs, which can interfere with driver cleanup.
Double-check that your Requisition and Product entities are properly annotated:
- Both must have
@Entityannotation - Both must define a valid
@Idprimary key field - No typos in table/column names that would break association resolution
内容的提问来源于stack exchange,提问作者Syed Nasrul Islam Anam

