简体   繁体   中英

TransactionSystemException thrown when fetching from MySQL db | java.lang.OutOfMemoryError: Java heap space

I am getting the following exception

[2017-05-22 22:02:43.930] boot - 8169 ERROR [http-nio-8080-exec-1] ---  TransactionInterceptor: Application exception overridden by rollback exception
   java.lang.OutOfMemoryError: Java heap space
     at com.mysql.jdbc.Buffer.<init>(Buffer.java:54)
     at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1972)
     at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:3285)
     at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:463)
     at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3009)
     at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2257)
     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2650)
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
     at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2002)
     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
     at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
     at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
     at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
     at org.hibernate.loader.Loader.doQuery(Loader.java:910)
     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
     at org.hibernate.loader.Loader.doList(Loader.java:2554)
     at org.hibernate.loader.Loader.doList(Loader.java:2540)
     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
     at org.hibernate.loader.Loader.list(Loader.java:2365)
     at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
     at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1718)
     at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
     at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:402)

whenever running the following method for a very specific entry:

public Person getPerson(String id) {
   Criteria criteria = this.getSession().createCriteria(Person.class);
   criteria.add(Restrictions.eq("id", id));
   Person p = (Person) criteria.uniqueResult();
   return p;
}

Increasing the memory does not seem to help, it just hangs.

Any ideas on how I can fix this? Loading other Person entities does not cause that issue.

Some maybe useful info:

 <springframework.version>4.2.1.RELEASE</springframework.version>
 <hibernate.version>4.3.11.Final</hibernate.version>
 <mysql.connector.version>5.1.36</mysql.connector.version>

Help please.

In short, I fixed the issue through Lazy loading.

My Person entity has references to several collections, which indeed where being fetched eagerly. The following annotation in all my collections solved the issue for me:

@ElementCollection(fetch = FetchType.LAZY)

Seems obvious now; my surprise since I've been running my app for quite a while now, loading Person entities on a regular basis, and never faced this issue before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM