Skip to content

Commit 150e7fc

Browse files
Hibernate ucp sample (oracle-samples#345)
* Sample hibernate application to configure ucp as a datasource * Update Readme.md * Update pom.xml * Made some fixes to the propeties * Made some fixes to the propeties * fixed the dependency scope * Fixed the properties in config file and proeprties file * Fixed ucp dependency * Removed hibernate.cfg.xml file and adjusted properties in hibernate.properties file. Updated dependency library versions in pom.xml
1 parent 4a5f71f commit 150e7fc

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

java/hibernate-ucp/Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Hibernate UCP Sample
2+
This is a hibernate sample that uses Universal Connection Pool(UCP). The sample creates an hibernate application and configures ucp through config/properties file..
3+
4+
# Main Components of the code
5+
* **HibernateUCPSample**: This is the main class where we have the methods to get a connection from UCP pool configured through the properties file.
6+
* **pom.xml**: Maven build script with all the necessary dependencies for the application to run.
7+
* **application.properties**: Contains all the database specific details such as database URL, database username, database password etc., This also contains all the properties to UCP as a datasource.

java/hibernate-ucp/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.oracle</groupId>
4+
<artifactId>hibernate-ucp-sample</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<properties>
7+
<maven.compiler.source>11</maven.compiler.source>
8+
<maven.compiler.target>11</maven.compiler.target>
9+
</properties>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jboss.logging</groupId>
14+
<artifactId>jboss-logging</artifactId>
15+
<version>3.5.3.Final</version>
16+
<scope>compile</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.hibernate</groupId>
20+
<artifactId>hibernate-core</artifactId>
21+
<version>6.2.2.Final</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.hibernate.orm</groupId>
25+
<artifactId>hibernate-testing</artifactId>
26+
<version>6.2.2.Final</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.oracle.database.jdbc</groupId>
31+
<artifactId>ojdbc11</artifactId>
32+
<version>23.4.0.24.05</version>
33+
<scope>compile</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.oracle.database.jdbc</groupId>
37+
<artifactId>ucp11</artifactId>
38+
<version>23.4.0.24.05</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.hibernate</groupId>
43+
<artifactId>hibernate-ucp</artifactId>
44+
<version>6.5.0.Final</version>
45+
</dependency>
46+
</dependencies>
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-surefire-plugin</artifactId>
52+
<version>3.1.2</version>
53+
</plugin>
54+
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.oracle.hibernate.ucp;
2+
3+
import org.hibernate.HibernateException;
4+
import org.hibernate.Session;
5+
import org.hibernate.SessionFactory;
6+
import org.hibernate.boot.Metadata;
7+
import org.hibernate.boot.MetadataSources;
8+
import org.hibernate.boot.registry.StandardServiceRegistry;
9+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10+
import org.hibernate.jdbc.Work;
11+
12+
import java.sql.Connection;
13+
import java.sql.SQLException;
14+
15+
public class HibernateUCPSample {
16+
17+
public static void main( String[] args ) throws HibernateException {
18+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
19+
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
20+
21+
SessionFactory factory = meta.getSessionFactoryBuilder().build();
22+
Session session = factory.openSession();
23+
24+
session.doWork(new Work() {
25+
public void execute(Connection con) throws SQLException {
26+
// Prints the UCP proxy class, indicating that UCP is configured as a datasource
27+
System.out.println("Connection class: " + con.getClass());
28+
}
29+
});
30+
31+
factory.close();
32+
session.close();
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# mandatory properties to configure UCP
2+
hibernate.connection.url=url
3+
hibernate.connection.username=user
4+
hibernate.connection.password=password
5+
hibenrate.connection.driver_class=oracle.jdbc.OracleDriver
6+
hibernate.oracleucp.connectionFactoryClassName=oracle.jdbc.datasource.impl.OracleDataSource
7+
8+
# all the following properties are optional, should be configured based on the requirement
9+
hibernate.oracleucp.initialPoolSize=3
10+
hibernate.oracleucp.minPoolSize=3
11+
hibernate.oracleucp.maxPoolSize=5
12+
hibernate.oracleucp.connectionPoolName=testucppool
13+
hibernate.oracleucp.dataSourceName=myDatabaseSource1
14+
15+
# this syntax has to be followed for properties
16+
hibernate.oracleucp.connectionProperties={autoCommit=false}
17+
#hibernate.oracleucp.connectionFactoryProperties={prop1=val1, prop2=val2, propN=valN}
18+
19+
hibernate.oracleucp.fastConnectionFailoverEnabled=false
20+
hibernate.oracleucp.validateConnectionOnBorrow=true
21+
hibernate.oracleucp.secondsToTrustIdleConnection=120
22+
hibernate.oracleucp.inactiveConnectionTimeout=180
23+
hibernate.oracleucp.maxStatements=20
24+
hibernate.oracleucp.connectionWaitTimeout=30

0 commit comments

Comments
 (0)