Spring JDBCTemplate + Transaction Manager内存泄漏

时间:2014-05-12 09:58:46

标签: java mysql spring memory-leaks

我真的很奇怪。我的Spring网络应用程序似乎有内存泄漏。 我使用的是Spring 3.2

我的Tomcat配置:

JAVA_OPTS="-Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=128m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/dumps"

我的dataSource声明:

<!-- declare transactionManager -->
<tx:annotation-driven  transaction-manager="transactionManager"/>
<beans:bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>

<beans:bean id="dataSource"  
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    <beans:property name="url" value="jdbc:mysql://localhost/test" />  
    <beans:property name="username" value="User" />  
    <beans:property name="password" value="password" />
    <beans:property name="initialSize" value="5" />
    <beans:property name="maxActive" value="55" />
    <beans:property name="maxIdle" value="20" />
    <beans:property name="minIdle" value="10" />
    <beans:property name="maxWait" value="10000" />
    <beans:property name="minEvictableIdleTimeMillis" value="55000" />
    <beans:property name="timeBetweenEvictionRunsMillis" value="34000" />
    <beans:property name="validationQuery" value="SELECT 1" />
    <beans:property name="testOnBorrow" value="true" />
    <beans:property name="removeAbandoned" value="true"/>
    <beans:property name="removeAbandonedTimeout" value="60"/>
    <beans:property name="logAbandoned" value="true"/>
</beans:bean>

<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>

我将jdbcTemplate注入dao。我也使用了Transaction-Annotations。

这是我从Memory Analizer(MAT)得到的:

  

类&#34; com.mysql.jdbc.NonRegisteringDriver&#34;,由&#34; org.apache.catalina.loader.StandardClassLoader @ 0x515c1390&#34;加载,占用758.845.296(72,57%) )字节。内存在&#34; java.util.concurrent.ConcurrentHashMap $ Segment []&#34;的一个实例中累积。由&#34;系统类加载器加载&#34;。

在细节视图中我看到了:

  

com.mysql.jdbc.NonRegisteringDriver $ ConnectionPhantomReference   14.360个对象中的前10个。物体数量:14.360。使用堆大小:459.520。保留堆大小:732.819.568

我发现了这个:bug report,但我使用的是5.1.29,所以我认为我不应该遇到这个问题。但也许问题出在apache dbcp中。有人能帮我吗?我的配置好吗? JDBCTemplate + TransactionManager中是否存在问题?

这是我的一项服务。我在服务中使用交易,可以吗?

@Service
public class ModerationServiceImpl implements ModerationService {
    @Autowired
    private EventService eventService;

    @Autowired
    private ContentManagementService cmService;

    @Autowired
    private IconDao iconDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void activateUserIcon(long userIconId, long userId, String comment)
        throws IconNotFoundException, NoPermissionException, IOException,
        IconNotAssignedToUserException, WrongIconStateException {
    if(!iconDao.isAssignedToUser(userIconId))
        throw new IconNotAssignedToUserException("icon: " + userIconId + " is not assigned to a user and can't be activated!");
    cmService.activateUserIcon(userIconId);
    eventService.writeUserIconEvent(userIconId, userId, new Date().getTime(), Enum_ServerEvent.verified, Enum_UserIconState.approved, comment);
}

这是我的DAO中的一个:

@Repository
public class IconDaoImpl extends DB_Contract implements IconDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public IconData getUserIconById(long iconId) throws IconNotFoundException {
    String sql = SELECT + UserIcon.CN_ServerID + NEXT +
            UserIcon.CN_State + NEXT +
            UserIcon.CN_Ref + 
            FROM + UserIcon.TABLE_NAME + WHERE + UserIcon.CN_ServerID + " = ?";
    try{
        return jdbcTemplate.queryForObject(sql, new UserIconRowMapper(), iconId);
    }catch (EmptyResultDataAccessException e){
        throw new IconNotFoundException("couldn't find icon for id: " + iconId);
    }
}

0 个答案:

没有答案