After (early all works ok) setting c3p0 connection pool to PostgreSQL DB like this:
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">500</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
can't realize PUT (only) requests. Hibernate works ok, but there are no records in DB. I tried different sets without result. What's wrong?
UPD: as we see in DB: PUT requests comes (they correct) but Hibernate rollback them. Why?
Find solution:
<property name="hibernate.c3p0.autoCommitOnClose">true</property>
It's works. But i'am not sure in it correction.
Related
I have created Java application using Hibernate with this configuration:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /bee</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Everything works fine when Iam using jdbc:mysql://localhost..., but now I need to have the database embedded in my application. Which database should I use? I need to have all my data stored and load it after start the application, update data, save, delete. I use HQL query or SQL query.
What is the simplest way to make the database embedded? I don´t want to change my queries. It would be fine to change only hibernate configuration and set it to the embedded database, is it possible?
One of the advantages of ORM's like Hibernate is to shield you from DB differences. You can use any one of the below as an embedded DB solution. Just change the dialect, driver and URL in hibernate cfg file.
H2
SQLite
HSQLDB
Hava a look at using the H2 database in embedded mode
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:~/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.default_schema">PUBLIC</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If you don't want to change your queries, consider mysql-mxj (embedded mysql) http://dev.mysql.com/doc/connector-mxj/en/connector-mxj.html
It is not under active development (thanks Oracle). But is prefectly usable and the connector is open source. It is trivial to embed different versions of mysql, but the latest mxj connector embeds 5.5.9
I am change my hibernate.cfg.xml added new options:
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="show_sql">true</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/pirates</property>
<property name="connection.username">postgres</property>
<property name="connection.password">mmm888</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">200</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>
<property name="hibernate.generate_statistics">true</property>
and added hibernate-c3p0-4.1.4.Final.yar in my project, but I'm not sure that I'm using c3p0. I can set hibernate.c3p0.max_size = 2 but Hibernate continues to create 400 ( 400 -for example) threads - if it needs to.
how to check that I am using c3p0 in hibernate ?
You are missing this line:
<property name="connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider"/>
This tells Hibernate to use the C3P0 connection pool.
Not sure the above answer is necessary, according to C3P0ConnectionProvider document:
https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/connection/C3P0ConnectionProvider.html
It says:
"A connection provider that uses a C3P0 connection pool. Hibernate will use this by default if the hibernate.c3p0.* properties are set."
Check here.
Generally speaking, applications should not have to configure a
ConnectionProvider explicitly if using one of the Hibernate-provided
implementations. Hibernate will internally determine which
ConnectionProvider to use based on the following algorithm:
If hibernate.connection.provider_class is set, it takes precedence
else if hibernate.connection.datasource is set → Using DataSources
else if any setting prefixed by hibernate.c3p0. is set → Using c3p0
else if any setting prefixed by hibernate.proxool. is set → Using
Proxool
else if any setting prefixed by hibernate.hikari. is set → Using
Hikari
else if hibernate.connection.url is set → Using Hibernate’s built-in
(and unsupported) pooling
else → User-provided Connections
Other than this, the fastest way to check is to go in database server and see whether the idle connection have been created or not.
Hi I have a small java project and am using hibernate with it. Now I would like to change the database to oracle, what changes would I have to do in configuration file to make it possible.
In you hibernate.cfg.xml, you should have a dialect defined. The line looks like this:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
change it to this:
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
Also change the database connection parameters in the same file.
Modify these properties. In hibernate.cfg.xml
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">your_new_password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#loalhost:xe</property>
<property name="hibernate.connection.username">user_name</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
I have been having trouble with Hibernate and Mysql timeout error.I am also using properties of c3p0(connection provider). After my Hibernate/MySQL have been running after 8 hours(which is default timeout value in Mysql), I have the exception. But it doesn't help.
property for auto reconnect also not working.
Here is my Hibernate Configuration:
<property name="connection_provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
**<property name="connection.autoReconnect"> true</property>
<property name="connection.autoReconnectForPools">true</property>**
<property name="connection.is-connection-validation-required">true</property>
<property name="c3p0.validate">true</property>
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_second_level_cache">false</property>
<property name="c3p0.idle_test_period">20</property>
<property name="c3p0.timeout">40</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.acquireRetryAttempts">10</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.maxIdleTime">300</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.minPoolSize">10</property>
<property name="c3p0.preferredTestQuery">select 1;</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/doqeap</property>
<property name="connection.user">root</property>
<property name="connection.password">*******</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.jdbc.batch_size">0</property>
<mapping></mapping>
please help me to sort out this problem.
Thanks
If the Connection timeout is the issue, then Connection testing should eliminate, wither via tests on checkout (reliable but imposes a client visible performance cost) or tests on checking + idle tests.
Looking at you config params, it looks like you mean to set tests on checkouts and idle tests. I'd expect that c3p0 would eliminate timed out Exceptions before your app saw them. If that hasn't happened, it'd be interesting to see two things: 1) c3p0's config, which gets logged at INFO when the pool is initialized -- is c3p0, through the hibernate layer, seeing the configuration you intend? 2) the Exception that your app receives when it encounters the stale Connections.
Good luck!
Hello everyone,
I am using hibernate ORM and oracle database. My cfg file has following properties:
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#url</property>
<property name="connection.username">username</property>
<property name="connection.password">pasword</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<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>
<property name="hibernate.c3p0.acquire_increment">3</property>
Everything works fine, but when I run the application and if I unplug network cable and plug it agian my db queries fail. It gives me the error
java.sql.SQLException: Io exception: Connection reset by peer: socket write error
Is there any way to re establish the connection?
You need to configure your database connection pool - not hibernate.Try setting idleConnectionTestPeriod and an appropriate preferredTestQuery, e.g., select 1 from dual.
See How To Configure The C3P0 ConnectionPool for more information. You'll get the most control if you create a c3p0.properties file in WEB-INF/classes but you need to make sure not to override those properties in your hibernate.cfg.xml.
Well I had written c3p0-config.xml like
<c3p0-config>
<default-config>
<!-- Configuring Connection Testing -->
<!-- property name="automaticTestTable">TEST_EMS_HIBERNATE_CONN</property -->
<property name="checkoutTimeout">0</property>
<property name="testConnectionOnCheckout">true</property>
<property name="testConnectionOnCheckin">false</property>
<property name="preferredTestQuery">SELECT 1 from dual</property>
<!-- Configuring Recovery From Database Outages -->
<property name="acquireRetryAttempts">0</property>
<property name="acquireRetryDelay">1000</property>
<property name="breakAfterAcquireFailure">false</property>
<!-- Configuring to Debug and Workaround Broken Client Apps -->
<property name="unreturnedConnectionTimeout">1800</property>
<property name="debugUnreturnedConnectionStackTraces">true</property>
</default-config>
and the system properties like:
C3P0_SYS_PROPS="-Dcom.mchange.v2.c3p0.cfg.xml=<FILE-PATH>/c3p0-config.xml -Dcom.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog -Dcom.mchange.v2.log.FallbackMLog.DE
FAULT_CUTOFF_LEVEL=WARNING"
As I see, you have specified when test connection, but have not specified how to test them. Read it http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing . I guess you should just add preferredTestQuery, usually it's something like SELECT 1 FROM DUAL.
Also read here Something wrong with Hibernate DB connection pooler c3p0