I want to create a one-shot database connection, based on spring classes.
To prevent spring loading the datasource on startup, I create it explicit when needed (only occasionally). I don't need any connection pooling, transactions and stuff like that.
DriverManagerDataSource ds = new DriverManagerDataSource(props.getUrl(), props.getUsername(), props.getPassword());
JdbcTemplate jdbc = new JdbcTemplate(ds);
jdbc.execute(...);
Question: how can I afterwards close/destroy the datasource and any open connections explicit?
Related
Using application.properties we can provide the
spring.datasource.hikari.maximum-pool-size=10
as spring using the Hikari connection pooling by default.
But when we are creating datasource manually as a specific requirement we cannot use the application.properties.
Scenario.
User will configure the datasource dynamically and that connection object should be available from thereafter.
For this we are creating DatasourceBuilder.create().build() method to create a datasource connection object and set it into the bean factory.
But while creating a datasource connection object using DataSourceBuilder.create().build() method it is creating connection pooling and 10 connection to the database at the same time.
We wanted to avoid that connection pooing and have only one connection.
How do I do that?
While creating the custom datasource we can create an instance of HikariDataSource instead of DataSource.
Assuming you are using Spring default Connection pooling (Hikari)
public DataSource createCustomConnection(String driverClass, String url, String username, String password) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClass);
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
config.setMaximumPoolSize(MENTIONED_TOTAL_CONNECTIONS);
// Like this you can configure multiple properties here
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
DataSource dataSource = createCustomConnection(...) // Pass Parameters here
Here you have created the datasource that creates a single connection at the same time.
for more information https://github.com/brettwooldridge/HikariCP#initialization
Thank you.
You can try setting the minimumIdle property. By default it is set to maxPoolSize, so 10 connections are created initially. You can refer minimumIdle section in this link, https://github.com/brettwooldridge/HikariCP#frequently-used
I'm trying to set up pooling with SQLServerDataSource if i understand this answer
https://stackoverflow.com/a/25573035/1262568
public DataSource dataSource() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.properties.getClassLoader())
.driverClassName(this.properties.getDriverClassName())
.url(this.properties.getUrl())
.username(this.properties.getUsername())
.password(this.properties.getPassword());
return factory.build();
}
Geting connection from a DataSource created in that way will return pooled connection using one of the available connection pools.
But what if instead DataSourceBuilder i want to use SQLServerDataSource
Will it also automatically use one of the available connection pool?
public DataSource dataSource() {
SQLServerDataSource sqlServerDataSource = new SQLServerDataSource();
sqlServerDataSource.setUser(UserName);
sqlServerDataSource.setPassword(Password);
sqlServerDataSource.setURL(Url);
return sqlServerDataSource;
}
Will it also automatically use one of the available connection pool?
No it won't. SQLServerDataSource is a SQL Server (driver) specific class, whereas DataSourceBuilder is a Spring class. Only the latter knows about Spring and its configuration and its configured connection pool.
Is there a reason you'd need to use SQLServerDataSource?
To access the native connection even from the pool, use
SQLServerConnection conn = connection.unwrap(SQLServerConnection.class);
just remember to call close() on connection and not conn, so the connection can be returned to the pool.
I have a doubt of how the spring boot JDBC template works. I have read the documentation , but could not understand clearly :( When does the template opens connection , when does it gets closed . How does the transactions are handled . Does it gets opened and closed for every query execution ?
When does the template opens connection, when does it gets closed
For building JdbcTemplate you should specify the JDBC DataSource to obtain connections from:
public JdbcTemplate(DataSource dataSource)
Or:
public JdbcTemplate()
JdbcAccessor.setDataSource(javax.sql.DataSource)
Conclusively, JdbcTemplate works with this DataSource.
DataSource, depending on the implementation, may return new standard Connection objects that are not pooled or Connection objects that participate in connection pooling which can be an be recycled.
JdbcTemplate has pooled connections and releases them back to DataSource.
How does the transactions are handled
JdbcTemplate relies on database transactions.
If you want to operate transactions on service layer/business logic you need transaction management.
The simplest way is to annotate services with #Transactional or use org.springframework.transaction.support.TransactionTemplate.
I am using spring-boot in my web application and use spring-jpa to read/write from/to my database. It works very well but I want to understand how to manage the database connections. Below is my properties configuration for database:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=user
spring.datasource.password=pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=500
I have set the maximum connections to 500. When a user makes a request on my spring application, a database connection will be opened for him. After finishing the request, will spring jpa close this connection? If not, when will it close the unused connections?
I have read through the spring jpa reference document from http://docs.spring.io/spring-data/jpa/docs/current/reference/html/. But it doesn't mention anything about the connections.
When using DB connection pooling, a call to sqlconnection.close() will not necessarily close the heavyweight connection to the database, instead most often will just release the connection as re-usable in the pool. That's why it is advisable to invoke the close() on connection as soon as possible when leveraging a client side connection pool.
In your configuration, the pool will contain a maximum number of 500 connections ( it would be also good to configure maxIdle, minIdle, and minEvictableIdleTimeMillis to tune the number of ready-to-use connections and how often to release them when not used).
Some more doc here
You have already found that you can configure this from application.properties
You can find all the possible properties here.
Notice that from Spring Boot 1.4 there are datasource properties for every datasource vendor that spring integrates with, out of the box. There is spring.datasource.dbcp.*,spring.datasource.tomcat.* and so on. See 1.4 docs
If that's not enought, and you need something very specific, you can declare the datasource bean yourself. Here is the example with Tomcat datasource:
#Bean
public DataSource dataSource(){
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
return datasource ;
}
To specify SQLite connection properties there is org.sqlite.SQLiteConfig and it goes something like this:
org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.setReadOnly(true);
config.setPageSize(4096); //in bytes
config.setCacheSize(2000); //number of pages
config.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
config.setJournalMode(SQLiteConfig.JournalMode.OFF);
SQLiteConnectionPoolDataSource dataSource = new SQLiteConnectionPoolDataSource();
Creating a connection pool with c3p0 goes something like this:
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("org.sqlite.JDBC");
cpds.setJdbcUrl("jdbc:sqlite:/foo/bar");
cpds.setMaxPoolSize(10);
Question: how do I create a DataSource that combines the two, letting me set things like the connection pool's max-pool-size and sqlite's synchronous mode?
Try
//put the imports where they really go, obviously...
import javax.sql.*;
import org.sqlite.*;
import com.mchange.v2.c3p0.*;
// configure SQLite
SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.setReadOnly(true);
config.setPageSize(4096); //in bytes
config.setCacheSize(2000); //number of pages
config.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
config.setJournalMode(SQLiteConfig.JournalMode.OFF);
// get an unpooled SQLite DataSource with the desired configuration
SQLiteDataSource unpooled = new SQLiteDataSource( config );
// get a pooled c3p0 DataSource that wraps the unpooled SQLite DataSource
DataSource pooled = DataSources.pooledDataSource( unpooled );
The DataSource pooled will now be a c3p0 PooledDataSource that wraps an SQLite unpooled DataSource which has been configured as you wish.
Please see c3p0's docs, "Using the DataSources factory class", and the API docs for the DataSources factory class.
See also the javadocs for SQLite JDBC, which I downloaded from here to answer this question.