Skip to content

Commit

Permalink
TRUNK-6136 - Add static method to retrieve the configured OpenMRS Dat… (
Browse files Browse the repository at this point in the history
openmrs#4150)

TRUNK-6136 - Add static method to retrieve the configured OpenMRS DataSource from Hibernate
  • Loading branch information
mseaton committed Sep 19, 2022
1 parent bb357d4 commit e82da79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions api/src/main/java/org/openmrs/api/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -1427,4 +1428,12 @@ public static void setUseSystemClassLoader(boolean useSystemClassLoader) {
public static boolean isUseSystemClassLoader() {
return getServiceContext().isUseSystemClassLoader();
}

/**
* @return a Connection from the OpenMRS database connection pool
* @since 2.5.7
*/
public static Connection getDatabaseConnection() {
return getContextDAO().getDatabaseConnection();
}
}
7 changes: 7 additions & 0 deletions api/src/main/java/org/openmrs/api/db/ContextDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.openmrs.api.db;

import java.sql.Connection;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -177,4 +178,10 @@ public interface ContextDAO {
* @see Context#updateSearchIndexForType(Class)
*/
public void updateSearchIndexForType(Class<?> type);

/**
* @return a Connection from the OpenMRS database connection pool
* @since 2.5.7
*/
public Connection getDatabaseConnection();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
*/
package org.openmrs.api.db.hibernate;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
Expand Down Expand Up @@ -48,6 +40,16 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;

/**
* Hibernate specific implementation of the {@link ContextDAO}. These methods should not be used
* directly, instead, the methods on the static {@link Context} file should be used.
Expand Down Expand Up @@ -547,4 +549,16 @@ public Future<?> updateSearchIndexAsync() {
throw new RuntimeException("Failed to start asynchronous search index update", e);
}
}

/**
* @see ContextDAO#getDatabaseConnection()
*/
public Connection getDatabaseConnection() {
try {
return SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
}
catch (SQLException e) {
throw new RuntimeException("Unable to retrieve a database connection", e);
}
}
}

0 comments on commit e82da79

Please sign in to comment.