Skip to content
Browse files

Fix for getting JNDI connection for different AS

Signed-off-by: Denis Bardadym <denis.bardadym@accenture.com>
  • Loading branch information...
1 parent 1c0980b commit f0097d8b104130fd9edf97524d23e61bd5dc3018 Denis Bardadym committed Sep 16, 2011
Showing with 18 additions and 2 deletions.
  1. +18 −2 persistence/db/src/main/scala/net/liftweb/db/DB.scala
View
20 persistence/db/src/main/scala/net/liftweb/db/DB.scala
@@ -73,7 +73,7 @@ trait DB extends Loggable {
*/
def jndiJdbcConnAvailable_? : Boolean = {
try {
- ((new InitialContext).lookup("java:/comp/env").asInstanceOf[Context].lookup(DefaultConnectionIdentifier.jndiName).asInstanceOf[DataSource].getConnection) != null
+ jndiConnection(DefaultConnectionIdentifier)!= null
} catch {
case e => false
}
@@ -142,13 +142,29 @@ trait DB extends Loggable {
clearThread(success)
}
}
+
+ private def jndiConnection(name: ConnectionIdentifier) : Connection = {
+ tryo {
+ (new InitialContext).lookup("java:/comp/env").asInstanceOf[Context].lookup(name.jndiName).asInstanceOf[DataSource].getConnection
+ } openOr {
+ tryo {
+ (new InitialContext).lookup("java:/comp/env/" + name.jndiName).asInstanceOf[DataSource].getConnection
+ } openOr {
+ tryo {
+ (new InitialContext).lookup(name.jndiName).asInstanceOf[DataSource].getConnection
+ } openOr {
+ throw new javax.naming.NameNotFoundException("DataSource for " + name + " not founded")
+ }
+ }
+ }
+ }
private def newConnection(name: ConnectionIdentifier): SuperConnection = {
val ret = ((threadLocalConnectionManagers.box.flatMap(_.get(name)) or Box(connectionManagers.get(name))).flatMap(cm => cm.newSuperConnection(name) or cm.newConnection(name).map(c => new SuperConnection(c, () => cm.releaseConnection(c))))) openOr {
Helpers.tryo {
val uniqueId = if (logger.isDebugEnabled) Helpers.nextNum.toString else ""
logger.debug("Connection ID " + uniqueId + " for JNDI connection " + name.jndiName + " opened")
- val conn = (new InitialContext).lookup("java:/comp/env").asInstanceOf[Context].lookup(name.jndiName).asInstanceOf[DataSource].getConnection
+ val conn = jndiConnection(name)
new SuperConnection(conn, () => {logger.debug("Connection ID " + uniqueId + " for JNDI connection " + name.jndiName + " closed"); conn.close})
} openOr {
throw new NullPointerException("Looking for Connection Identifier " + name + " but failed to find either a JNDI data source " +

0 comments on commit f0097d8

Please sign in to comment.
Something went wrong with that request. Please try again.