Should I open a JDBC connection for each rest call?
I am dealing with high traffic in my Spring Boot project and my goal is serving clients as much fast as possible. In this case, I have more than 500 requests per second. In each rest endpoint call, I should connect my schema and gather multiple information from multiple tables. To be able to do that, should I create new connection for each eendpoint call or create & close before each db query?
I wrote a JDBC connection class but I am not sure that it is a good way. Maybe you can give me some opinion.
JDBC Connection Class
@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;
}
}
DAO
@Component
public interface FraudCommTransactionsDao {
Long count();
}
DAO IMPL
@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;
}
}
java jdbc
|
show 3 more comments
I am dealing with high traffic in my Spring Boot project and my goal is serving clients as much fast as possible. In this case, I have more than 500 requests per second. In each rest endpoint call, I should connect my schema and gather multiple information from multiple tables. To be able to do that, should I create new connection for each eendpoint call or create & close before each db query?
I wrote a JDBC connection class but I am not sure that it is a good way. Maybe you can give me some opinion.
JDBC Connection Class
@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;
}
}
DAO
@Component
public interface FraudCommTransactionsDao {
Long count();
}
DAO IMPL
@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;
}
}
java jdbc
2
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
1
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
1
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you usingDriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.
– Mark Rotteveel
Jan 19 at 10:47
|
show 3 more comments
I am dealing with high traffic in my Spring Boot project and my goal is serving clients as much fast as possible. In this case, I have more than 500 requests per second. In each rest endpoint call, I should connect my schema and gather multiple information from multiple tables. To be able to do that, should I create new connection for each eendpoint call or create & close before each db query?
I wrote a JDBC connection class but I am not sure that it is a good way. Maybe you can give me some opinion.
JDBC Connection Class
@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;
}
}
DAO
@Component
public interface FraudCommTransactionsDao {
Long count();
}
DAO IMPL
@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;
}
}
java jdbc
I am dealing with high traffic in my Spring Boot project and my goal is serving clients as much fast as possible. In this case, I have more than 500 requests per second. In each rest endpoint call, I should connect my schema and gather multiple information from multiple tables. To be able to do that, should I create new connection for each eendpoint call or create & close before each db query?
I wrote a JDBC connection class but I am not sure that it is a good way. Maybe you can give me some opinion.
JDBC Connection Class
@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;
}
}
DAO
@Component
public interface FraudCommTransactionsDao {
Long count();
}
DAO IMPL
@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;
}
}
java jdbc
java jdbc
edited Jan 19 at 10:46
Mark Rotteveel
60k1476120
60k1476120
asked Jan 18 at 17:38
BerkinBerkin
465620
465620
2
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
1
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
1
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you usingDriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.
– Mark Rotteveel
Jan 19 at 10:47
|
show 3 more comments
2
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
1
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
1
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you usingDriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.
– Mark Rotteveel
Jan 19 at 10:47
2
2
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
1
1
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
1
1
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you using
DriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.– Mark Rotteveel
Jan 19 at 10:47
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you using
DriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.– Mark Rotteveel
Jan 19 at 10:47
|
show 3 more comments
2 Answers
2
active
oldest
votes
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection
object created with DriverManager.getConnection()
is a physical connection while DataSource.getConnection()
returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
add a comment |
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258938%2fshould-i-open-a-jdbc-connection-for-each-rest-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection
object created with DriverManager.getConnection()
is a physical connection while DataSource.getConnection()
returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
add a comment |
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection
object created with DriverManager.getConnection()
is a physical connection while DataSource.getConnection()
returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
add a comment |
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection
object created with DriverManager.getConnection()
is a physical connection while DataSource.getConnection()
returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection
object created with DriverManager.getConnection()
is a physical connection while DataSource.getConnection()
returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
edited Jan 19 at 11:43
answered Jan 19 at 11:00
Karol DowbeckiKarol Dowbecki
20.4k92852
20.4k92852
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
add a comment |
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
Whilst this answer is correct - it is the answer to an XY problem. The OP needs to follow a basic Spring tutorial before playing around with connection pools. Spring handles all of this by default.
– Boris the Spider
Jan 19 at 11:02
add a comment |
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
add a comment |
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
add a comment |
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
answered Jan 18 at 18:42
Arif HosainArif Hosain
624
624
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258938%2fshould-i-open-a-jdbc-connection-for-each-rest-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
this is a bad idea, use a datasource instead.
– Sleiman Jneidi
Jan 18 at 17:40
Thanks, I will look it up.
– Berkin
Jan 18 at 17:45
1
Please read about Spring Boot and connection pooling: docs.spring.io/spring-boot/docs/current/reference/htmlsingle
– Simon Martinelli
Jan 18 at 18:15
Move the connection cache inside the datasource. Look into c3p0.
– Thorbjørn Ravn Andersen
Jan 18 at 18:21
1
Spring Boot provides datasources with connection pools and the right methods for providing environment specific configurations. Why are you using
DriverManager
? And why are you trying to do in code what Spring Boot can already provide for you? And to be blunt, Spring Boot itself handles this better than your code does.– Mark Rotteveel
Jan 19 at 10:47