Home > Blockchain >  Spring boot jpa findbyid() stops working after restart of application
Spring boot jpa findbyid() stops working after restart of application

Time:01-03

I've got the following problem:

If i run my application for the "first time" so there are no tables in my database, spring creates them and everything works fine. I can do save() and findbyId() and so on.

If i restart the application, it hangs up on the first findbyId() statement and I don't know why.

I'm using

  • spring boot 3.0.1 (latest version)
  • mariadb 10.9
  • java 17

This is my code:

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.1</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    .
    .
    .
</dependencies>

application.properties

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mariadb://${MYSQL_HOST:localhost}:3306/database
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.open-in-view=false

Entity

@Entity
@Table(name = "guild_entity")
@Getter
@Setter
public class GuildEntity {

    @Id
    @Column(name = "guild_id")
    private String guildId;

    @Column(name = "bot_control_role")
    private String botControlRole;

    @Column(name = "command_prefix")
    @Builder.Default
    private String commandPrefix = "varchar(1) default '&'";

    @Column(name = "language")
    @Builder.Default
    private String language = "varchar(2) default 'EN'";

    @Column(name = "voice_control_role")
    private String voiceControlRole;

    @Column(name = "volume")
    @Builder.Default
    private Integer volume = 20;

    @Column(name = "player_shuffle")
    @Builder.Default
    private Boolean shuffle = false;

    @Column(name = "player_repeat")
    @Builder.Default
    private Boolean repeat = false;

    @Column(name = "write_channel")
    private String writeChannel;

    @Column(name = "enable_support_system")
    @Builder.Default
    private Boolean enableSupportSystem = false;

    @Column(name = "enable_welcome_messages")
    @Builder.Default
    private Boolean enableWelcomeMessages = false;

    @Column(name = "welcome_channel")
    private String welcomeChannel;

    @Column(name = "welcome_message")
    @Builder.Default
    private String welcomeMessage = "varchar(255) default ':wave: Welcome to %server%, %user%!'";
}

EntityRepository

@Repository
public interface GuildEntityRepository extends JpaRepository<GuildEntity, String> {

}

SQLHandler

@Service
@RequiredArgsConstructor
public class SQLHandler {

    @Autowired
    private final GuildEntityRepository guildEntityRepository;

    public GuildEntity getGuild(String guildId) {
        NeptuneBot.getLogger().debug(guildId, "Get guild", LogType.SQL);
        return guildEntityRepository.findById(guildId).orElse(null);
    }

    public void saveAllGuilds(Iterable<GuildEntity> guildEntities) {
        NeptuneBot.getLogger().debugSQL("Save all guild entities...");
        guildEntityRepository.saveAll(guildEntities);
        NeptuneBot.getLogger().debugSQL("Done!");
    }
}

Logs after restart

[2m2022-12-31T03:07:38.523 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[0.1-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
    [2m2022-12-31T03:07:38.523 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[0.1-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
    [2m2022-12-31T03:07:38.525 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[0.1-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 1 ms
    [2m2022-12-31T03:07:38.907 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[           main][0;39m [36mnet.dv8tion.jda.api.JDA                 [0;39m [2m:[0;39m Login Successful!
    [2m2022-12-31T03:07:39.198 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[inWS-ReadThread][0;39m [36mn.d.j.internal.requests.WebSocketClient [0;39m [2m:[0;39m Connected to WebSocket
    [2m2022-12-31T03:07:39.608 01:00[0;39m [32m INFO[0;39m [35m1751594[0;39m [2m---[0;39m [2m[inWS-ReadThread][0;39m [36mnet.dv8tion.jda.api.JDA                 [0;39m [2m:[0;39m Finished Loading!
    [2022.12.31 03:07:42]: SQL | Get guild
    Hibernate: 
        select
            g1_0.guild_id,
            g1_0.bot_control_role,
            g1_0.command_prefix,
            g1_0.enable_support_system,
            g1_0.enable_welcome_messages,
            g1_0.language,
            g1_0.player_repeat,
            g1_0.player_shuffle,
            g1_0.voice_control_role,
            g1_0.volume,
            g1_0.welcome_channel,
            g1_0.welcome_message,
            g1_0.write_channel 
        from
            guild_entity g1_0 
        where
            g1_0.guild_id=?

Thanks for your help and a happy new year!

CodePudding user response:

It was completely my fault, I had a NullPointerException on a method but it wasn't thrown for some reason. Everything seems to be fine with my spring configuration.

Thanks for your help!

  • Related