Java 我应该为每个rest调用打开JDBC连接吗?

Java 我应该为每个rest调用打开JDBC连接吗?,java,jdbc,Java,Jdbc,我正在处理我的Spring Boot项目中的高流量问题,我的目标是尽可能快地为客户服务。在这种情况下,我每秒有500多个请求。在每个rest端点调用中,我应该连接我的模式并从多个表中收集多个信息。要做到这一点,我应该为每个eendpoint调用创建新连接,还是在每个db查询之前创建并关闭连接 我编写了一个JDBC连接类,但我不确定这是否是一个好方法。也许你能给我一些意见 JDBC连接类 刀 DAO IMPL 使用application.properties文件的最简单方法可以快速获取数据并交付给

我正在处理我的Spring Boot项目中的高流量问题,我的目标是尽可能快地为客户服务。在这种情况下,我每秒有500多个请求。在每个rest端点调用中,我应该连接我的模式并从多个表中收集多个信息。要做到这一点,我应该为每个eendpoint调用创建新连接,还是在每个db查询之前创建并关闭连接

我编写了一个JDBC连接类,但我不确定这是否是一个好方法。也许你能给我一些意见

JDBC连接类 刀 DAO IMPL
使用application.properties文件的最简单方法可以快速获取数据并交付给客户机。您可以使用它来获得到数据源的数据库连接。

否,建立到数据库服务器的新物理连接的成本很高。它涉及多个步骤:用户授权、建立会话默认值、在客户端和服务器上分配内存等。不应将此开销添加到每个请求中

创建共享应用程序线程之间的物理连接是一种常见做法。这引入了逻辑连接的概念,例如使用
DriverManager创建的
Connection
对象。getConnection()
是物理连接,而
DataSource.getConnection()
返回作为代理的逻辑连接


您可以使用多个Java数据库连接池库,例如。不要自己写,这是。

这是个坏主意,改用数据源。谢谢,我会查一下。请阅读有关Spring启动和连接池的内容:将连接缓存移到数据源中。查看c3p0。SpringBoot为数据源提供了连接池和提供特定于环境的配置的正确方法。为什么要使用
DriverManager
?为什么您要尝试在代码中实现Spring Boot已经为您提供的功能?坦率地说,Spring Boot本身比您的代码处理得更好。虽然这个答案是正确的,但它是XY问题的答案。OP在使用连接池之前需要遵循基本的Spring教程。默认情况下,Spring处理所有这些。
@PropertySource({"classpath:application.properties"})
@Configuration
public class FraudJDBConfiguration {
    private final Logger LOGGER = LogManager.getLogger(FraudJDBConfiguration.class);

    private final Environment env;

    @Autowired
    public FraudJDBConfiguration(Environment env) {
        this.env = env;
    }

    @Bean
    public Connection getFraudConnection() {
        // Step 1: Loading or
        // registering Oracle JDBC driver class
        String connectionClass = env.getProperty("fraud.db.driver-class-name");
        try {
            Class.forName(connectionClass);
        } catch (ClassNotFoundException cnfex) {
            LOGGER.error(cnfex.getMessage());
            throw new RuntimeException("JDBC driver class'ı bulunamadı");
        }

        // Step 2: Opening database connection
        try {
            String environmentType = env.getProperty("environment");
            if (environmentType == null) {
                LOGGER.error("environment Tip Hatası (TEST - UAT - LIVE)");
                throw new RuntimeException("environment Tip Hatası (TEST - UAT - LIVE)");
            } else {
                String connectionString = null;
                String username = null;
                String password = null;
                switch (environmentType.toLowerCase()) {
                    case "dev":
                        connectionString = env.getProperty(/*someurl*/);
                        username = env.getProperty(/*someusername*/);
                        password = env.getProperty(/*somepassword*/);
                        break;
                    case "tst":
                        connectionString = env.getProperty(/*someurl*/);
                        username = env.getProperty(/*someusername*/);
                        password = env.getProperty(/*somepassword*/);
                        break;
                    case "liv":
                        connectionString = env.getProperty(/*someurl*/);
                        username = env.getProperty(/*someusername*/);
                        password = env.getProperty(/*somepassword*/);
                        break;
                    case "uat":
                        connectionString = env.getProperty(/*someurl*/);
                        username = env.getProperty(/*someusername*/);
                        password = env.getProperty(/*somepassword*/);
                        break;
                }
                // Step 2.A: Create and
                // get connection using DriverManager class
                if (connectionString == null) {
                    LOGGER.error("fraud şeması için connection string bulunamadı");
                    throw new RuntimeException("fraud şeması için connection string bulunamadı");
                }
                return DriverManager.getConnection(connectionString, username, password);
            }
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }
}
@Component
public interface FraudCommTransactionsDao {
    Long count();
}
@Service
public class FraudCommTransactionsDaoImpl implements FraudCommTransactionsDao {
    private final FraudJDBConfiguration fraudJDBConfiguration;

    @Autowired
    public FraudCommTransactionsDaoImpl(FraudJDBConfiguration fraudJDBConfiguration) {
        this.fraudJDBConfiguration = fraudJDBConfiguration;
    }

    @Override
    public Long count() {
        try(Connection connection = fraudJDBConfiguration.getFraudConnection()) {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(/*some query*/);

            if (rs.next()) {
                return rs.getLong("transaction_id");
            } else {
                return 0L;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}