Created
November 7, 2020 15:34
-
-
Save niroj-office/389d7f9b0da08f0f76a628fb284cd1a5 to your computer and use it in GitHub Desktop.
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
{ | |
... | |
... | |
var connection: Option[Connection] = None | |
var resultSet: Option[ResultSet] = None | |
var statement: Statement = null | |
var savepoint: Savepoint = null | |
try { | |
Class.forName(driver) | |
try {connection = Some(DriverManager.getConnection(url, user, password)) | |
} catch { | |
case e: Exception => logger.info(s"connection=$connection") | |
e.printStackStrace() | |
} | |
statement = connection.get.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) | |
connection.get.setAutoCommit(false) | |
savepoint = connection.get.setSavepolnt(randomSvPoint) | |
lower.info("Spltting queries into SQL Array...") | |
val queryArray = queries.split(";+").map(_.trim) | |
if (queryArray.length == 1) { | |
if (internalQuery.trim.toUpperCase.startsWith("SELECT ")) | |
resultSet = Some(statement.executeQuery(internalQuery)) | |
else { | |
statement.executeDpdate(InternalQuery) | |
connection.get.commit() | |
} | |
) else { | |
var i = 0 | |
var counter = 0 | |
var lastQuery = "" | |
logger.Info("Adding all quries...") | |
queryArray.filterNot(_.trim.isEmpty).foreach { q => | |
counter += 1 | |
if (q.toLowerCase.trim == "commit" || counter % commitNbr == 0 ) { | |
numUpdates = statement.executeBatch() | |
i += numUpdates.size | |
logger.info(s"Till now $i updates completed") | |
if ( q.toLowerCase.trim == "commit" ) | |
logger.info("Commiting as per explicit commit passed ...") | |
else | |
logger.info(s"Comming at DB after an interval of $commitNbr records ...") | |
connection.get.commit() | |
} else statement.addBatch(q) | |
lastQuery = q.toLowerCase.trim | |
} | |
numUpdates = statement.executeBatch() | |
numUpdates.foreach { r => | |
i += 1 | |
if ( r == statement.SUCCESS_NO_INFO) logger.info(s"Execution $i : Unknown number of rows updated !") | |
} | |
connection.get.commit //Final commit | |
logger.info(s"Total $i rows updated") | |
System.setProperty("TOTAK_SUCC_DB_UPDT", i.toString) | |
} | |
} catch { ... } | |
finally { | |
if (!resultSet.isDefined) connection.isDefined && statement != null) | |
statement.close | |
} | |
if(resultSet.isDefined) resultSet | |
else None | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment