Setting JDBC connection/socket timeout via persistence.xml - oracle

I'm trying to set JDBC timeout via persistence.xml, but it doesn't work.
This is how my persistence.xml looks like:
<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>OracleDataSourceExample</jta-data-source>
<properties>
<property name="oracle.net.READ_TIMEOUT" value="1"/>
<property name="oracle.jdbc.ReadTimeout" value="1"/>
<property name="oracle.net.CONNECT_TIMEOUT" value="1"/>
</properties>
</persistence-unit>
So, does anyone have suggestion what to do? (how to set jdbc timeout)

Related

Using JPA to connect oracle database,but it still shows Could not load requested class : oracle.jdbc.driver.OracleDriver

I use JPA configuration file,persistence.xml,to set the connection info below:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.vo.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin://localhost:1521/orcl" />
<property name="javax.persistence.jdbc.user" value="cuser" />
<property name="javax.persistence.jdbc.password" value="cuser" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
And I have added ojdbc8.jar in Maven repository:
https://i.imgur.com/FGIlQxg.png
And I import ojdbc8.jar in Maven dependecy:
https://i.imgur.com/bkXg65L.png
And I also set ojdbc8.jar info in pom.xml:
https://i.imgur.com/koleE8Y.png
But when I try to connect to oracle database,it just shows
java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver
and I see the ojdbc8.jar in Maven dependecy ,the ojdbc8.jar surely contains oracle.jdbc.driver.OracleDriver
What happened about this?HOW Can I do to fix it?
Try Using
oracle.jdbc.OracleDriver
instead of
oracle.jdbc.driver.OracleDriver
This is what worked for me. Check your URL, which is not correct.
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.url" value=""jdbc:oracle:thin:#myhost:1521/myorcldbservicename"/>

Accessing a datasource from an OSGi bundle

I have a blueprint file containing a datasource deployed to Apache ServiceMix. I was able to query the datasource from Apache Karaf console. How can I access this datasource from a Camel Spring-DM bundle application? This is my blueprint file:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="URL"/>
<property name="user" value="USER"/>
<property name="password" value="PASSWORD"/>
</bean>
<service interface="javax.sql.DataSource" ref="dataSource" id="ds">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/ds"/>
</service-properties>
</service>
</blueprint>
You can bind the DataSource as an OSGi service. In spring dm this is osgi:reference, in blueprint it would be reference.
<reference id="dataSource" interface="javax.sql.DataSource"/>
You can then inject the DataSource for example into the SqlComponent.
As an example see a fix I did for this camel route. This is blueprint but it is almost the same for spring dm.
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>
Using Hibernate as JPA provide:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="jpa" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>osgi:service/jdbc/ds</jta-data-source>
...
</persistence-unit>
</persistence>

OpenJPA disabling create tables on startup

I must use an external database in a spring non web application.
How can i disable openjpa to try to create entity tables? My entities EmailAddress and Message has annotations "#Entity", i do not know if it is right.
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>pack.EmailAddress</class>
<class>pack.Message</class>
<properties>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/kepsDb" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="openjpa.jdbc.SynchronizeMappings" value="false"/>
</properties>
</persistence-unit>
Remove the openjpa.jdbc.SynchronizeMappings property for your persistence.xml file.

Spring and Hibernate - changing dialect

In our web app that uses Spring and Hibernate, the hibernate configuration is in the META-INF/persistence.xml, but there is one problem, we are using two different databases, one for testing and other one for production.
Here is our `persistence.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="SpringMVCTest" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/jdbc/sqliteDS</jta-data-source>
<class>pl.meble.taboret.model.UserEntity</class>
<class>pl.meble.taboret.model.WordList</class>
<class>pl.meble.taboret.model.WordUnit</class>
<class>pl.meble.taboret.model.ActivateUserAccountPermaLink</class>
<class>pl.meble.taboret.model.ResetPasswordPermaLink</class>
<class>pl.meble.taboret.question.QuestionUnit</class>
<class>pl.meble.taboret.question.OpenQuestion</class>
<class>pl.meble.taboret.question.MultipleChoiceQuestion</class>
<class>pl.meble.taboret.question.WithGapsQuestion</class>
<class>pl.meble.taboret.question.QuestionList</class>
<class>pl.meble.taboret.answer.AnswerUnit</class>
<class>pl.meble.taboret.answer.OpenQuestionAnswer</class>
<class>pl.meble.taboret.answer.MultipleChoiceQuestionAnswer</class>
<class>pl.meble.taboret.answer.WithGapsQuestionAnswer</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="pl.meble.taboret.utils.SQLiteDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<!-- <property name="hibernate.connection.driver_class" value="${database.driver}"
/> <property name="hibernate.connection.url" value="${database.url}" /> -->
<!-- <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> -->
<!--<property name="hibernate.transaction.factory_class" value="com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory"/> -->
<property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
so, is it possible to change the value of hibernate.properties at runtime, or store this value for example in JNDI resource?
Or is there some other way to conditionally set hibernate.dialect, so for example for testing we would have SQLite dialect and for normal deploy he would use Postgre dialect.
Yes. In spring you define the entity manager with a bean:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
You can configure properties of that bean:
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
where ${hibernate.dialect} is spring property. So you can pass the property when starting your project (either via -Dhibernate.dialect, or by placing it in a properties file and loading it with <context:property-placeholder-configurer>

Persistence 1.0/2.0 XSD is down?

I've created a spring project which I later deployed on glassfish, the ap is working at first. But when I added persistence.xml it took mins to be done with error.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="sidoPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDatasource</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.JTATransactionFactory" />
</properties>
</persistence-unit>
</persistence>
When browse: http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd, it wouldn't load.
Any idea or work around on this issue?
I think it's related with the whole java.net, java.sun.com, project Kenai etc. being down because of the maintenance reasons.
https://blogs.oracle.com/theaquarium/entry/outage_on_java_net_apr

Resources