Created
January 12, 2021 22:56
-
-
Save apinela/eb545ad8944ceecb80b8af9598458ba9 to your computer and use it in GitHub Desktop.
Java Method to get postgresql server version by using an hibernate session.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static boolean validatePostresqlServerVersionGreaterOrEqualTo(short major, short minor) { | |
short actualMajor = 0; | |
short actualMinor = 0; | |
try { | |
Session session = HibernateUtil.getSession(); | |
session.beginTransaction(); | |
SQLQuery query = session.createSQLQuery("SELECT VERSION()"); | |
query.setReadOnly(true); | |
query.setFetchSize(1); | |
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); | |
results.next(); | |
String postgresVersionResult = (String) results.get(0); | |
// Expected to have something like: | |
// PostgreSQL 9.2.23 on x86_64-unknown-linux-gnu.... | |
// Get the second string and parse major and minor | |
postgresVersionResult = postgresVersionResult.split(" ")[1].trim(); | |
String[] temp = postgresVersionResult.split("\\."); | |
actualMajor = Short.parseShort(temp[0]); | |
actualMinor = Short.parseShort(temp[1]); | |
if (actualMajor >= major && actualMinor >= minor) | |
return true; | |
} | |
catch (Exception ex) { | |
logger.info("Unable to get PostgreSQL Server Version."); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment