Spring Boot JPA ManyToMany导致OutOfMemoryError

问题描述 投票:0回答:1

我有一个Spring boot应用程序,我正在尝试插入与国家/地区有关的电影,但是几分钟后引发了异常。简化实体:

电影:

public class Movie implements Serializable {

    ...

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    @JoinTable(name = "movies_countries",
        joinColumns = @JoinColumn(name = "id_movie"),
        inverseJoinColumns = @JoinColumn(name = "id_country")
    )
    private Set<Country> countries = new HashSet<>();

    public Set<Country> getCountries() {
        return this.countries;
    }

    public void setCountries(Set<Country> countries) {
        this.countries = countries;
    }

    public void addCountry(Country country) {
        getCountries().add(country);
        country.getMovies().add(this); // Point where it gets blocked
    }
    ...
}

国家:

public class Country implements Serializable {

    ...

    @ManyToMany(mappedBy = "countries")
    private Set<Movie> movies = new HashSet<>();

    public Set<Movie> getMovies() {
            return movies;
    }

    public void setMovies(Set<Movie> movies) {
        this.movies = movies;
    }

    ...

}

以及将电影与国家/地区联系起来的方法:

private void insertCountries(final List<String> countries, final EntityManager entityManager, final Movie movie) {
    for (String strCountry: countries) {
        Country country = this.getCountry(entityManager, strCountry);
        movie.addCountry(country);
    }
}

Exception:

Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space
    at io.netty.buffer.PooledDirectByteBuf$1.newObject(PooledDirectByteBuf.java:35)
    at io.netty.buffer.PooledDirectByteBuf$1.newObject(PooledDirectByteBuf.java:32)
    at io.netty.util.Recycler.get(Recycler.java:158)
    at io.netty.buffer.PooledDirectByteBuf.newInstance(PooledDirectByteBuf.java:40)
    at io.netty.buffer.PoolArena$DirectArena.newByteBuf(PoolArena.java:786)
    at io.netty.buffer.PoolArena.allocate(PoolArena.java:145)
    at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:332)
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185)
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:176)
    at io.netty.channel.nio.AbstractNioChannel.newDirectBuffer(AbstractNioChannel.java:448)
    at io.netty.channel.nio.AbstractNioByteChannel.filterOutboundMessage(AbstractNioByteChannel.java:275)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:881)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1365)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:816)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:723)
    at io.netty.handler.logging.LoggingHandler.write(LoggingHandler.java:249)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:816)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:723)
    at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:106)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)
    at io.netty.channel.AbstractChannelHandlerContext.access$1700(AbstractChannelHandlerContext.java:38)
    at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.write(AbstractChannelHandlerContext.java:1127)
    at io.netty.channel.AbstractChannelHandlerContext$WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1174)
    at io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.run(AbstractChannelHandlerContext.java:1098)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:495)
2020-03-02 12:41:54,741 WARN  [http-nio-8090-exec-2] com.zaxxer.hikari.pool.PoolBase: HikariPool-1 - Failed to validate connection com.mysql.jdbc.JDBC4Connection@35e38de8 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.

要尝试,我尝试将内存从1 Gb增加到2 Gb,但是仍然会发生。并非在所有胶片插入中都发生这种情况。

有什么建议吗?

java spring-boot jpa out-of-memory
1个回答
0
投票

许多关系非常棘手,不要在它们之间使用级联配置,级联与保存实体有关,这里您没有实体,只有一个关联,该关联由orm监视进行管理基础集合中发生的事情,如果您看一下实现,则该集合实际上是某种工具包,可用于检测其中的更改。保存必须在拥有的一面:没有mappedBy注释的那面进行。

© www.soinside.com 2019 - 2024. All rights reserved.