Grails run-app:在新连接上执行任意 SQL
在生产中,我们使用 JBoss 和 JNDI 来连接数据库。在 JNDI 配置中,我们有如下内容:
<new-connection-sql>ALTER SESSION SET CURRENT_SCHEMA=XXX</new-connection-sql>
对于开发,我将 run-app
与默认 Tomcat 容器 (Grails 1.3.2) 一起使用,并使用硬编码的 DataSources 参数。到目前为止,在我的开发配置中使用 default_schema
参数就足以近似上述的 alter session 语句。但由于使用了一些原生 Sql 语句,这已经不够了。我不想更改 SQL 来满足我的开发设置(例如,通过在运行时提取 default_schema
值)。我看到两个选项:
- 也许有一个等效的 new-connection-sql 参数可以在我的开发配置中使用。
- 如果没有,也许我可以转而在我的开发环境中使用 JNDI。那没问题,但是环顾四周,我找不到关于如何做到这一点的可靠说明。事实上,人们似乎遇到了麻烦。此外,我不确定 Web 容器之间是否平等地接受所有 JNDI 配置,以及 Tomcat 是否特别支持某种
new-connection-sql
参数。
谁能帮我找出解决这个问题的最佳方法?谢谢!
编辑
这是我正在使用的本机Sql代码(我们的目标是使用触发器更新DATE_CREATE列的旧数据库,并且必须在保存时以这种方式检索它):
def getCreationDate() {
if (!creationDate) {
def sql = new Sql(dataSource)
sql.query("select DATE_CREATED from some_view where id=?", [id]) {ResultSet rs ->
rs.next()
creationDate = rs.getTimestamp("DATE_CREATED")
}
}
return creationDate
}
In production, we use JBoss with JNDI for connecting to the database. In the JNDI configuration, we have something like:
<new-connection-sql>ALTER SESSION SET CURRENT_SCHEMA=XXX</new-connection-sql>
For development, I use run-app
with the default Tomcat container (Grails 1.3.2) using hard-coded DataSources arguments. Up until now it has been sufficient to approximate the above alter session statement using the default_schema
parameter in my development configuration. But this is no longer sufficient due to the use of some native Sql statements. I do not want to change the SQL to cater to my development setup (e.g. by extracting the default_schema
value at runtime). I see two options:
- Perhaps there is an equivalent
new-connection-sql
argument I can use in my development configuration. - If not, perhaps I can move to using JNDI in my development environment. That would be OK, but looking around I have not been able to find solid instructions on how to do this. In fact, people seem to be having trouble with it. Moreover, I'm unsure whether all JNDI configurations are accepted equally between web containers and whether Tomcat specifically supports some kind of
new-connection-sql
argument.
Can anyone help me figure out the best way to go about this? Thanks!
Edit
Here is the native Sql code I am using (we are targeting a legacy database which uses a trigger to update the DATE_CREATE column and must retrieve it in this fashion on save):
def getCreationDate() {
if (!creationDate) {
def sql = new Sql(dataSource)
sql.query("select DATE_CREATED from some_view where id=?", [id]) {ResultSet rs ->
rs.next()
creationDate = rs.getTimestamp("DATE_CREATED")
}
}
return creationDate
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论