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

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.

Related

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 create the database through Hibernate

In my spring project, i am using Hibernate to export my entity classes to a previously created database. But this will require the final user knows how to create a database in the Database manager system (Currently I am using Postgresql).
Is there any way of, given only the machine where the postgresql is installed (and the username and password, which is provided when the application is runned the first time), the Hibernate create a new database in the server if it doesn't exist?
If your configuration looks like this
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://host:port/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Then the database will be created by Hibernate automatically.
Update:
Ok now I understand what you want. You want to start the Postgresql server with Hibernate. This is not possible. Hibernate does not do this.
You can do this with
Another script that starts with your application
A maven/ant target.
A build job
But the best solution is to use an in-memory database that does not need an external server (for example H2, or Java derby)
See also
Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
and
Postgres database create if not exists
Take a look of paramater hibernate.hbm2ddl.auto for your hibernate.cfg.xml file. I suggest you this link: Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?
Run "CREATE DATABASE ..." (see http://www.postgresql.org/docs/9.0/static/sql-createdatabase.html) as a native SQL query ...
.createSQLQuery(" ... ").executeUpdate(); ...
Hibernate will - at least as far as I know - not create the database, only the tables in it.
I suppose you need to connect to postgresql via a second persistence unit/connection, because of the chicken-and-egg nature of this approach.

Enable connection pooling Spring ibatis

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">

how to configure cache in hibernate with jboss? ? And test as well in kumud console?

Does any one know, how to configure cache for hibernate with jboss ?
My clear question is I am using JPA and Jboss. Every time I call JPA method its creating entity and binding query.
My persistence properties are
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
And I am creating entity manager the way shown below:
emf = Persistence.createEntityManagerFactory("pu");
em = emf.createEntityManager();
em = Persistence.createEntityManagerFactory("pu")
.createEntityManager();
Is there any nice way to manage entity manager resource insted create new every time or any property can set in persistance. Remember it's JPA.
The question is not clear, there are many second level cache providers for Hibernate and they are not application server specific.
To enable the second level cache, you need to set the following properties in Hibernate configuration file hibernate.cfg.xml:
<property name="hibernate.cache.use_second_level_cache">true</property>
And if you want to also enable query result caching:
<property name="hibernate.cache.use_query_cache">true</property>
Then, declare the name of a class that implements org.hibernate.cache.CacheProvider - a cache provider - under the hibernate.cache.provider_class property. For example, to use JBoss Cache 2:
<property name="hibernate.cache.provider_class">org.hibernate.cache.jbc2.JBossCacheRegionFactory</property>
Of course, the JAR for the provider must be added to the application classpath.
That's for the Hibernate side. Depending on the chosen cache provider, there might be additional configuration steps. But as I said, there are many second level cache providers: EHCache, JBoss Cache, Infinispan, Hazelcast, Coherence, GigaSpace, etc.

how do I change persistence.xml at run time

I am new to openJPA.
I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml.
For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.
And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.
FYI I am using WS app. server 7 and RAD 7.5
Any type of help would be highly appreciated.
You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.
Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.
Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.
For example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:#localhost:1521:XE
Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.
Of course, you can use JNDI data sources with Spring also.

Categories

Resources