I need to configure connection pooling for the DB in our API. We are using Hikari as a data source.
Based on the references provided here - https://stackoverflow.com/a/47653776/9246275. and https://stackoverflow.com/a/28988541/9246275
It's recommended to go with fixed-size pool. But, How does the configuration looks like?
I don't see a parameter with the name - fixed size pool
Could you please help me with what should I need to remove and added that is suggested?
<?xml version='1.0' encoding='UTF-8'?>
<bean id="DataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" lazy-init="true">
<property name="driverClassName" value="${}"/>
<property name="jdbcUrl" value="${}"/>
<property name="username" value="${}"/>
<property name="password" value="${}"/>
<property name="poolName" value="${pname}"/>
<property name="idleTimeout" value="10000"/>
<property name="maximumPoolSize" value="20"/>
<property name="connectionTimeout" value="10000"/>
</bean>
Related
I have implemented spring jdbc in my project. I am just curious to know how connection pooling in handled in spring jdbc? If spring is taking care of connections, then where can I specify the max number of connections allowed for my application?
Another question is how is connection pooling handled in simple jdbc.
Please clarify.
You can use own custom datasource, like this:
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:SPRING_TEST" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
//Dao class configuration in spring
<bean id="EmployeeDatabaseBean" class="com.test.EmployeeDAOImpl">
<property name="dataSource" ref="springDataSource"/>
</bean>
Official documentation
Here or here described pretty simple.
I currently use this configuration for my projects:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="URL" ...
<property name="user" ...
<property name="password" ...
<property name="connectionCachingEnabled" value="true" />
And it works fine, pretty fast.
I happened to see, on an old project (spring 2.5) this configuration:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url"...
<property name="username" ...
<property name="password" ...
</bean>
From documentation it would seem that this last option does not make use of a connection pool. I see no reason to use this configuration over mine, but it still exists so I am curious: where's the advantage/limitation?
1st configuration is oracle specific, whereas 2nd configuration is generic. You can explicitly define driver class.
This is the only major difference I can see in them other than connection pool support of OracleDataSource.
You can use it for generic behavior as mentioned below:
<bean id="baseDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
abstract="true">
<property name="username" value="user"/>
<property name="password" value="pwd" />
</bean>
<bean id="mySqlDataSource" parent="baseDataSource">
<property name="driverClassName" value="${mySQL.driver}" />
<property name="url" value="${mySQL.url}"/>
</bean>
<bean id="oracleDataSource" parent="baseDataSource">
<property name="driverClassName" value="${oracle.driver}" />
<property name="url" value="${oracle.url}"/>
</bean>
Property values you can externalized.
You can explore Apache Jakarta Commons DBCP which has all the features of DriverManagerDataSource
along with connection pool feature.
Our application currently uses a datasource which is defined in the JBoss standalone.xml, and basically we need to have this be defined within the app rather than in the container for a while. Our current setup is;
application-context.xml;
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="rtsPersistenceUnit" />
<property name="packagesToScan">
...
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
</bean>
</property>
</bean>
persistance.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="rtsPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/RTSdb</jta-data-source>
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.transaction.flush_before_completion" value="true" />
</properties>
</persistence-unit>
</persistence>
datasource.xml:
<bean id="rtsDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="..."/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
Basically all I want is for the line
<jta-data-source>java:/jdbc/RTSdb</jta-data-source>
to read from datasource.xml rather than go the container (JBoss).
It seems like it would be simple but after reading some Spring and Oracle docs I couldn't find an answer.
Yes, you could use a JTA compliant transaction manager like Atomikos or Bitronix. Their respective sites have documentation on how to configure them with Spring. In general, you will have to follow the steps given below (if using Atomikos):
Retain your existing XA data source (rtsDatasource in your case) or create one if not already using (for example, if someone has a non-XA data source, that data source must be converted to an XA data source first).
Wrap the XA data source in an AtomikosDataSourceBean.
Point your EntityManagerFactory at the new AtomikosDataSourceBean instance.
Declare an XA transaction manager and an XA user transaction.
Wrap the XA transaction manager in a Spring JtaTransactionManager.
Use the Spring JtaTransactionManager.
A short configuration snippet using H2 database, Hibernate 4, Spring 4 and Atomikos 4 is shown below.
<bean class="org.h2.jdbcx.JdbcDataSource" id="originalDataStore" lazy-init="true">...</bean>
<bean class="com.atomikos.jdbc.AtomikosDataSourceBean" id="dataSource" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="xaDS"/>
<property name="xaDataSource" ref="originalDataStore"/>
<property name="poolSize" value="3"/>
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
...
</props>
</property>
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
<property name="transactionManager">
<bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false"/>
</bean>
</property>
<property name="userTransaction">
<bean class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300"/>
</bean>
</property>
<property name="allowCustomIsolationLevels" value="true"/>
</bean>
<transaction:annotation-driven transaction-manager="transactionManager"/>
For details, you can see this app.
Say I have the following bean:
<bean id="some-bean" class="com.icyrock.example.SomeBean">
<property name="pa1" value="va1"/>
<property name="pa2" value="va2"/>
<property name="pa3" value="va3"/>
<property name="pa4" value="va4"/>
<property name="pa5" value="va5"/>
<property name="pa5">
<bean class="com.icyrock.example.SomeOtherBean>
<property name="px1" value="vx1"/>
<property name="px2" value="vx2"/>
<property name="px3" value="vx3"/>
<property name="px4" value="vx4"/>
<property name="px5" value="vx5"/>
</bean>
</property>
</bean>
I'd like to separate this into blocks along the lines:
<block id="b1">
<property name="pa1" value="va1"/>
<property name="pa2" value="va2"/>
<property name="pa3" value="va3"/>
</block>
<block id="b2">
<property name="pa4" value="va4"/>
<property name="pa5" value="va5"/>
</block>
<block id="b3">
<property name="px1" value="vx1"/>
<property name="px2" value="vx2"/>
</block>
<block id="b4">
<property name="px3" value="vx3"/>
<property name="px4" value="vx4"/>
<property name="px5" value="vx5"/>
</block>
<bean id="some-bean" class="com.icyrock.example.SomeBean">
<block-ref id="b1"/>
<block-ref id="b2"/>
<property name="pa5">
<bean class="com.icyrock.example.SomeOtherBean>
<block-ref id="b3"/>
<block-ref id="b4"/>
</bean>
</property>
</bean>
where block and block-ref are imaginary Spring tags just to illustrate the idea.
Is there a way to do this without:
Changing the Java classes (e.g. to accept the map of properties or a subclass)
Using anything not already in Spring itself (i.e. anything along the lines of building some factories/property-setters/whatever else to use in Spring context file(s) or so)
The purpose would be to reuse definitions (i.e. blocks) without making any relationships (e.g. parent-child relationships or things like that). As an example, this is standard JDBC data-source definition:
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
(example from here). If there are different servers that are accessed, it can be the case that they share the same driver (so driverClassName would be the shared) and it also can be the case they use the same credentials (so username and password would be the shared). I'd like to do something like:
<block id="driver-credentials">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</block>
<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url1}"/>
<block-ref id="driver-credentials"/>
</bean>
<bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url2}"/>
<block-ref id="driver-credentials"/>
</bean>
<bean id="ds3" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url3}"/>
<block-ref id="driver-credentials"/>
</bean>
or something along these lines. Obviously this can be a parent-child for this simple example, I'm just wondering about a mixin-type solution. Pretty much the same thing Spring offers for multiple files, except on the bean level.
Opinions on why doing the above would not be good or alternative ways to do it are welcome.
Parent child with merge = true on child is the one supported by spring. merge does act as mixin (in a way for collections), right?
I've a problem with the Flyway schema upgrader for Spring. Following code exists in my servlet.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/database"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate" depends-on="dataSource">
<property name="dataSource" ref="dataSource"/>
</bean>
But how to set the migration script directory or rather whats the default directory?
Everything is explained in the excellent documentation:
If you want to alter the default directory (which is db/migration as shown on the picture above), I believe this should work (see: Flyway.setBaseDir()):
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate" depends-on="dataSource">
<property name="dataSource" ref="dataSource"/>
<property name="baseDir" value="my/migrations/path"/>
</bean>