这两天,服务器跑起来就挂,报内存溢出,当然这不是第一次了。之前是因为为了统计一个数字,代码中写的是查询出来List然后统计List的Size,改正后解决问题。
那么这次从各种角度去考虑问题了,可是还是不行,启动后不久就报错:
java.lang.OutOfMemoryError: Java heap space at java.util.AbstractList.iterator(AbstractList.java:273) ~[na:1.6.0_43] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.convertToMetaConstraints(AnnotationMetaDataProvider.java:240) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findPropertyMetaData(AnnotationMetaDataProvider.java:222) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getPropertyMetaData(AnnotationMetaDataProvider.java:216) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:129) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:117) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfigurationForHierarchy(AnnotationMetaDataProvider.java:99) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:160) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:128) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:292) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:133) ~[hibernate-validator-4.3.1.Final.jar:4.3.1.Final] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:136) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:102) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:228) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:115) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:287) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final] at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75) ~[hibernate-entitymanager-4.2.3.Final.jar:4.2.3.Final] at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513) ~[spring-orm-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755) ~[spring-tx-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) ~[spring-tx-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) ~[spring-tx-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) ~[spring-tx-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) ~[spring-tx-3.2.3.RELEASE.jar:3.2.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.3.RELEASE.jar:3.2.3.RELEASE]
因为是启动后不久,我担心是内存泄漏,随后注销了ehcache缓存,可是不使用缓存,照样出问题。
我看了数据库机器的网络使用情况,发现一直是很高,说明这次和上次的问题应该是一样的,还是大量查询了List集合。
后来同事给两个SpringJPA查询代码改正了一下,这两行代码,理论上返回一行数据,但是并没有处理。于是增加上处理,只让他返回一行数据,而不是靠理论上。
原来的SQL:
@Query("select a from PhoneUser a where (a.imei = ?1 and a.imsi = ?2) or (a.imei2 = ?1 and a.imsi2 = ?2)")
可以看出,如果不是理论上,那和之前的问题就真的一样了,总是返回大量数据,修正后代码:
@Query("select a from PhoneUser a where ((a.imei = ?1 and a.imsi = ?2) or (a.imei2 = ?1 and a.imsi2 = ?2)) and rownum<2")
因为使用的是Oracle数据库,增加rownum属性,使数据库确切返回的是一条数据。
如此解决了问题。
因此,在实际编程中,要注意以下几点:
(1)不能依靠理论上来限制问题的发生。
(2)永远要分页,哪怕一页数据多些。
(3)不要过度依靠框架编程。
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: