카테고리 없음

timeout 처리

진열사랑 2024. 8. 7. 18:31

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;



import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.Statement;



@SpringBootApplication

public class SpringBootPostgresApplication implements CommandLineRunner {



    @Autowired

    private DataSource dataSource;



    public static void main(String[] args) {

        SpringApplication.run(SpringBootPostgresApplication.class, args);

    }



    @Override

    public void run(String... args) {

        try (Connection connection = dataSource.getConnection()) {

            try (Statement statement = connection.createStatement()) {

                // Set statement_timeout to 30 seconds

                statement.execute("SET statement_timeout TO '30s'");

                // Now execute your query

                statement.execute("SELECT * FROM your_table");

            }

            System.out.println("Successfully executed the query!");

        } catch (SQLException e) {

            System.err.println("Failed to execute the query: " + e.getMessage());

        }

    }

}