Enable connection pooling Spring ibatis - java

I am using spring Ibatis for database management in my java application.I need to enable connection pooling to increase the performance of the application.
i added following properties to SqlMapConfig.xml file to enable the connection pooling
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/xxxxx"/>
<property name="JDBC.Username" value="xxxxxx"/>
<property name="JDBC.Password" value="xxxxxxx"/>
<property name="Pool.MaximumActiveConnections" value="50"/>
<property name="Pool.MaximumIdleConnections" value="20"/>
</dataSource>
</transactionManager>
But i couldn't find any visible performance changes.Do i need to make any other changes or settings changes to enable the connection pooling?.
Following JAR files are added in my build path
ibatis-2.3.4.726.jar
ibatis2-common-2.1.6.589.jar
ibatis2-dao-2.1.6.589.jar

I also noted that you cannot set a minimum set of connections during startup. So 50 simultaneous requests at start of your test will all try to setup a database connection. Just as in the single connection case. Try using a real connection pool implementation with a DataSource. Or make sure you are testing your performance after pool has been filled.
Try using a real pool implementation. JDBC pool is quite popular and avoids some of the locking issues older pooling implementations have. See http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html.
Also measure your performance and make sure you really are using connection pooling. I have seen sharing of one connection over a whole application which also could explain what you are experiencing.

Try using, dataSource type POOLED instead of SIMPLE.
<dataSource type="POOLED">

Related

Recover from idle in transaction with postgresql and BasicDataSource

I run postgresql with a transactional application where the stack is:
postgresql
tomcat
hibernate/spring
This is a production application where we have a bunch of customers connected at once. Each customer has it's own postgresql database but each customer also has many users.
Occasionally, i will get a situation where one of the customer databases locks up and I see idle in transaction tied to the processes for this customer.
postgres: customer1 customer1 127.0.0.1(59738) idle in transaction
When the database locks up the other databases continue to work fine. I can not get this to unlock without restarting the server application.
The problem is often triggered by various long running reports that the customer runs. I believe it is a locking/blocking issue where other users are also accessing the same data in the customer database.
This problem happens rarely and I can't ever reproduce it. But when it does happen it is a serious issue. Mostly I just want to recover from it.
Postgresql seems to function fine itself when this occurs. Like I can perform psql to the database and run queries. So, I think the problem or rather the solution centers around the data source.
I use the apache commons BasicDataSource.
<bean id="customer1DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/customer1"/>
<property name="username" value="customer1"/>
<property name="password" value="password"/>
</bean>
So does anyone know of a setting in the basic data source that will kill these idle in transaction connections, throw an exception or allow them to recover somehow?

Why is hibernate.connection.release_mode after_transaction not recommended for JTA?

While analyzing some performance problems in Wildfly 10.1 in high pressure scenarios I came to the conclusion, that sometimes parallel HTTP threads block each other.
The reason seemed to be that in some HTTP requests we execute two JPQL Queries (actually a delete and a select) and sometimes the second of the two simply didn't get a JDBC connection from the pool. (We use IBM DB2, if that is important...) That seemed rather ridiculous as the first statement already got a connection.
After reading the Hibernate docs, I see that the default for hibernate.connection.release_mode is after_statement and that after_transaction is not recommended for JTA apps...
So... I have a few questions now:
Why does after_statement ever make sense? (unless you have auto_comit on of course...)
Why shouldn't I use after_transaction in JTA apps?
Is my assumption correct that after_transaction should fix the described issue?
Any help is appreciated!
Problems with connection pool management can occur at several levels, database, hibernate, application server. In order to facilitate this management, it is strongly recommended to use C3p0 which is a Java library that provides a convenient way for managing database connections.
See document C3Po link
You can then configure in your hibernate.cfg.xml file the release mode of JTA transactions. It is best to leave it at the default value: "auto", as shown below:
<property name="hibernate.connection.release_mode">auto</property><!-- Release mode database connection- auto the default value -->
<property name="hibernate.c3p0.max_statements">0</property> <!-- Number of prepared statements will be cached. 0 no caching -->
<property name="hibernate.c3p0.max_size">1000</property> <!-- Max connection pool size -->
<property name="hibernate.c3p0.min_size">50</property> <!-- Min connection pool size -->
<property name="hibernate.c3p0.timeout">550</property> <!-- Time in seconds before free a connection. 0 no expiration -->
<property name="hibernate.c3p0.idleConnectionTestPeriod">1800</property> <!-- idle time in seconds before testing if a connection is valid - Setting a fairly long idleConnectionTestPeriod, and not testing on checkout and check-in at all is an excellent, high-performance approach. -->
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>

how to set particular value for c3p0's property in hibernate?

I'm using hibernate for my web application and it's working fine. I have set the properties of connection pooling like below.
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
I have set min_size = 5, max_size=20, max_statements=50
but it could be min_size=1, max_size=100, max_statements=500
so, at what basis should I set these values? I have read some tutorials
about hibernate connection pooling but didm't get any specific idea
how to set these properties' values
that totally depends on how much load your application have, to check how much db activities are happening on c3p0 side you have to monitor the internal stats of objects and you need JMX to see that stats and based on that you can manage and configure pool, plz check below link
Configuring and Managing c3p0 via JMX
I would also recommend you to Check HikariCP, as its way much better than c3p0.

Embedded H2 + Tomcat 7 JDBC realm + Hibernate = "Locked by another process"

Perhaps the title is self explanatory, but I am trying to create a web application with an embedded instance of the H2 database. I am configuring Tomcat 7 to use the JDBC realm to form-based authentication. server.xml has:
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.h2.Driver"
connectionURL="jdbc:h2:/someDir/myDB"
connectionName="userName"
connectionPassword="password"
userTable="user_enabled"
userNameCol="user_name"
userCredCol="pass"
userRoleTable="user_role"
roleNameCol="role_name" />
I am also using Hibernate for persistence. persistence.xml has:
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:/someDir/myDB" />
<property name="hibernate.connection.username" value="userName" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
<property name="current_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext" />
If I try to start the server I get:
(...) Caused by: org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process".
And my web application fails to start.
Starting an H2 TCP server and replacing JDBC URLs with jdbc:h2:tcp://myIP/ works fine. So no problem with my database files. Also, I am sure nothing else is trying to use those files. So the conflict can only be between the JDBC realm and Hibernate.
Why do I want an embedded server? For the classic reasons: I want to distribute this application and I don't want users to have to start two processes instead of just one. Also, this will not be a shared database, so no need to create a new process just for that. Finally, no additional server ports are open for database, which is good for security.
Answering my own question, the way to make this work is by using a JNDI data source to access the database. For authentication, Tomcat should be configured to use the DataSourceRealm. So both Tomcat security and Hibernate will use the same embedded instance of H2, which does not cause the conflict I was experiencing.
Though the JNDI data source is preferable, the original issue is that you are trying to create two connections to the same H2 database using a file or embedded database url. H2 does support this, but both of the processes would need to have ";AUTO_SERVER=true" appended to the connection url. This would cause the first connection to start the db in process in embedded mode, but would allow the seccond connection to connect through tcp ip.
Details are available here:
http://www.h2database.com/html/features.html#auto_mixed_mode

Can't see JMX entries in jconsole when using Tomcat JDBC Connection Pool

we're evaluating switching over from the C3P0 connection pool to the Tomcat JDBC Connection Pool (as described here).
It appears to work as a connection pool but I can't seem to see any JMX entries for it when I run jconsole.
Out of the box C3P0 gives lots of operations and attributes via JMX, the Tomcat
JDBC Connection Pool gives none (for me).
According to the page linked above there is a jmxEnabled flag that defaults to true. I've set this explicitly but it seems to make no difference.
What am I missing?
I'm running a fairly standard Java6/Spring/Hibernate app by the way.
If you configure pool in your spring context, you should export bean manually. Autoexport works only if you confiugre pool in tomcat container and import it from JNDI. See http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#JMX
You may use this spring config for export pool information to JMX:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
... skipped ...
</bean>
<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=DataSource" value="#{dataSource.getPool().getJmxPool()}"/>
</map>
</property>
</bean>
Config works only in Spring version 3.0 and above, because it uses spring expression language
Do you see any entries under the tree
Catalina -> DataSource -> javax.sql.DataSource
This lists the number of active connections, idle connections, and a few other common stats.
Other then that, what kind of information are you hoping to get out of monitoring?
Darren, if you don't see the object name under Catalina/DataSource/javax.sql.DataSource/<name> in JConsole, then I wonder if the datasource is defined incorrectly or perhaps it was not connected to the database when Tomcat was started.
Bruce
In support of Sean's post:
The placement of the javax.sql.DataSource entry in MBeans of JConsole is:
Catalina
DataSource
/[Name_of_deployed_application] // e.g. "/DomainService
/[Host_name_of_the_deployed_application] //e.g. "localhost"
javax.sql.DataSource

Categories

Resources