I am trying to access a Datasource specified in the Tomcat server.xml and context.xml by play. The playapplication is located as war within the tomcat webapps and also has the Connection in its web.xml specified with:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/Testconnection</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
To see hat the connection is specified I used a jsp to check the configuration:
Context initialContext = new InitialContext();
Context componentBindings = (Context) initialContext.lookup("java:comp/env");
DataSource dataSource = (DataSource) componentBindings.lookup("jdbc/Testconnection");
Connection connection = dataSource.getConnection();
String connectionUrl = connection.getMetaData().getURL();
out.println(connectionUrl);
This showed the exspected result.
When trying to access the database within the play application it always results in:
"could not find datasource for ....: Token not allowed in path expression: '-'" pointing to the line containing DB.withConnection
in my play app.
So what is confusing me is that there is not such a token as '-' within my whole code.
I tried to access the following combinations:
DB.withConnection("java:comp/env/jdbc/Testconnection") { imlicit c => .......}
DB.withConnection("java:jdbc/Testconnection") { imlicit c => .......}
DB.withConnection("jdbc/Testconnection") { imlicit c => .......}
Would be great if anybody can provide me with a solution or point me in the right direction.
Thank you very much!
Solution:
So the Solution (thanks to applicius!) could be:
import javax.naming.{Context, InitialContext}
import javax.sql.DataSource
[...]
var conn: java.sql.Connection = (new InitialContext).lookup("java:/comp/env").asInstanceOf[Context].lookup("jdbc/Testconnection").asInstanceOf[DataSource].getConnection
val result: Option[Result] = SQL(queryName).on(("variable","replaceValue")).singleOpt(Result.rowMapper)(conn)
[...]
Play DB API is expecting a Play datasource name (one corresponding to entry in Play application.conf
), not a JNDI name.
Either you get connection by your self using regular JNDI lookup and use it in your Play app (having to release it by yourself), or you move/duplicate datasource settings into Play config.