Skip to content

Commit 3b39cc9

Browse files
committed
Sample hibernate application to configure ucp as a datasource
1 parent 3ed5f41 commit 3b39cc9

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

java/hibernate-ucp/Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 though 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.
8+

java/hibernate-ucp/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.3.0.23.09</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.oracle.database.jdbc</groupId>
36+
<artifactId>ucp</artifactId>
37+
<version>23.3.0.23.09</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.hibernate</groupId>
41+
<artifactId>hibernate-ucp</artifactId>
42+
<version>6.5.0.CR1</version>
43+
</dependency>
44+
</dependencies>
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-surefire-plugin</artifactId>
50+
<version>3.1.2</version>
51+
</plugin>
52+
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.cfg.Configuration;
11+
import org.hibernate.jdbc.Work;
12+
13+
import oracle.ucp.UniversalConnectionPool;
14+
import oracle.ucp.admin.UniversalConnectionPoolManager;
15+
import oracle.ucp.admin.UniversalConnectionPoolManagerImpl;
16+
17+
import java.sql.Connection;
18+
import java.sql.SQLException;
19+
20+
public class HibernateUCPSample {
21+
22+
public static void main( String[] args ) throws HibernateException {
23+
Configuration configuration = new Configuration();
24+
configuration.configure("hibernate.cfg.xml");
25+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
26+
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
27+
28+
SessionFactory factory = meta.getSessionFactoryBuilder().build();
29+
Session session = factory.openSession();
30+
31+
try{
32+
UniversalConnectionPoolManager mgr = UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
33+
UniversalConnectionPool pool = mgr.getConnectionPool("testucppool");
34+
System.out.println(pool.getName());
35+
System.out.println(pool.getAvailableConnectionsCount());
36+
}
37+
catch(Exception e) {
38+
e.printStackTrace();
39+
}
40+
41+
session.doWork(new Work() {
42+
public void execute(Connection con) throws SQLException {
43+
System.out.println("Connection class: " + con.getClass());
44+
}
45+
});
46+
47+
factory.close();
48+
session.close();
49+
}
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5+
6+
<hibernate-configuration>
7+
<session-factory>
8+
<property name="hbm2ddl.auto">create</property>
9+
<property name="connection.url">jdbc:oracle:thin:@//phoenix93464.dev3sub2phx.databasede3phx.oraclevcn.com:5521/cdb1_pdb1.regress.rdbms.dev.us.oracle.com</property>
10+
<property name="connection.username">system</property>
11+
<property name="connection.password">manager</property>
12+
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
13+
<property name="oracleucp.connectionFactoryClassName">oracle.jdbc.pool.OracleDataSource</property>
14+
<property name="oracleucp.minPoolSize">1</property>
15+
<property name="oracleucp.maxPoolSize">5</property>
16+
<property name="oracleucp.connectionPoolName">testucppool</property>
17+
</session-factory>
18+
</hibernate-configuration>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
hibernate.connection.url=jdbc:oracle:thin:@//phoenix93464.dev3sub2phx.databasede3phx.oraclevcn.com:5521/cdb1_pdb1.regress.rdbms.dev.us.oracle.com
2+
hibernate.connection.username=system
3+
hibernate.connection.password=manager
4+
hibernate.show_sql=true
5+
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
6+
7+
hibernate.hbm2ddl.auto=create
8+
hibernate.dialect=org.hibernate.dialect.OracleDialect
9+
10+
hibernate.oracleucp.connectionFactoryClassName=oracle.jdbc.pool.OracleDataSource
11+
hibernate.oracleucp.minPoolSize=3
12+
hibernate.oracleucp.maxPoolSize=5
13+
hibernate.oracleucp.connectionPoolName=testucppool
14+
15+
hibernate.oracleucp.serverName=myServer1
16+
hibernate.oracleucp.portNumber=1000
17+
hibernate.oracleucp.databaseName=myDatabaseName1
18+
hibernate.oracleucp.dataSourceName=myDatabaseSource1
19+
hibernate.oracleucp.description=myDescription1
20+
hibernate.oracleucp.networkProtocol=myNetworkProtocol1
21+
hibernate.oracleucp.roleName=myroleName1
22+
23+
#this syntax has to be followed for properties
24+
hibernate.oracleucp.connectionProperties={autoCommit=false}
25+
#hibernate.oracleucp.connectionFactoryProperties={prop1=val1, prop2=val2, propN=valN}
26+
27+
hibernate.oracleucp.validateConnectionOnBorrow=true
28+
hibernate.oracleucp.SQLForValidateConnection=select * from dual
29+
hibernate.oracleucp.initialPoolSize=3
30+
hibernate.oracleucp.abandonedConnectionTimeout=305
31+
hibernate.oracleucp.timeToLiveConnectionTimeout=310
32+
hibernate.oracleucp.inactiveConnectionTimeout=400
33+
hibernate.oracleucp.maxIdleTime=315
34+
hibernate.oracleucp.timeoutCheckInterval=320
35+
hibernate.oracleucp.propertyCycle=325
36+
hibernate.oracleucp.maxStatements=405
37+
hibernate.oracleucp.connectionWaitTimeout=330
38+
hibernate.oracleucp.maxConnectionReuseTime=335
39+
hibernate.oracleucp.maxConnectionReuseCount=340
40+
hibernate.oracleucp.connectionHarvestTriggerCount=345
41+
hibernate.oracleucp.connectionHarvestMaxCount=350
42+
hibernate.oracleucp.fastConnectionFailoverEnabled=false
43+
hibernate.oracleucp.ONSConfiguration=myOnsConfig1
44+
hibernate.oracleucp.secondsToTrustIdleConnection=410
45+
hibernate.oracleucp.connectionLabelingHighCost=415
46+
hibernate.oracleucp.connectionRepurposeThreshold=420
47+
hibernate.oracleucp.highCostConnectionReuseThreshold=430
48+
hibernate.oracleucp.maxConnectionsPerShard=435
49+
50+
# not available on 18c version
51+
hibernate.oracleucp.shardingMode=true
52+
hibernate.oracleucp.connectionValidationTimeout=440
53+
54+
# not available on 19c version
55+
hibernate.oracleucp.readOnlyInstanceAllowed=false

0 commit comments

Comments
 (0)