创建连接时执行sql语句

发布于 2024-12-27 09:29:13 字数 396 浏览 1 评论 0原文

为了使我们的应用程序正常工作,我们需要在每个新连接上执行 SQL 语句,然后再将该连接分发给应用程序。

如何在 WAS 7 中相应配置数据源?

我们找到了(已弃用的)选项来使用 sql 语句验证数据源,这有望达到目的(同事正在测试它)。这听起来是错误的,因为我们不是“测试”连接,而是正确设置它。而且它已被弃用,因此这可能会停止与 websphere 的未来版本一起使用

是否有一个干净且正确的方法来执行此操作?

我们想要执行的语句是

ALTER SESSION NLS_SORT='GERMAN_AI'

一种替代方法:应用程序是基于 hibernate 的,因此如果我们可以说服 hibernate 在使用连接之前执行该语句,那么这也可以工作。

For our application to work properly we need to execute a SQL Statement on every new connection, before that connection is handed out to the application.

How do I configure a data source in WAS 7 accordingly?

We found the (deprecated) option to validate the datasource using a sql statement, which hopefully does the trick (coworker is testing it right now). This sounds wrong, since we are not 'testing' the connection, but setting it up properly. Also its deprecated so this probably will stop working with future versions of websphere

Is there a clean and correct way to do this?

The statement we'd like to execute is

ALTER SESSION NLS_SORT='GERMAN_AI'

One alternative approache: The application is hibernate based, so if we could convince hibernate to execute the statement before using a connection, this would work as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山川志 2025-01-03 09:29:13

如果是我,我只会使用“连接测试”方法:

  • 它有效!
  • YAGNI 原则说“当它发生时担心弃用......如果 它曾经发生过” - 可能几年后或永远不会
  • 通过找到“正确”的方式您不会增加任何业务价值
  • 您可以放弃它并继续一些真正的真正为您的项目增加价值的工作

唯一的缺点是它会每次测试连接时都会执行,这在连接的生命周期中可能会执行很多次,但那又怎样 - 这是一个非常快的执行语句并且是幂等的,所以没问题。

If it were me, I would just use the "connection test" approach:

  • It works!
  • The YAGNI principle says "worry about deprecation when it happens... if it ever happens" - probably years away or never
  • You will not add any business value by finding the "correct" way
  • You can drop this and get on with some real work that actually adds value to your project

The only downside is that it will be executed every time a connection is tested, which may be many times during the life of the connection, but so what - it's a very fast executing statement and is idempotent, so no problem.

心安伴我暖 2025-01-03 09:29:13

无论如何,您不是 WAS 专家,但如果您可以设置 Tomcat JDBC 来提供数据库连接池,那么它有一个名为“initSQL”的参数。您可以将其设置为希望连接池在创建连接时运行的 SQL 语句。

Tomcat JDBC 连接池

Not a WAS expert by any means, but if you can set up Tomcat JDBC to provide your database connection pooling, it has a parameter amongst others called "initSQL". You can set that to a SQL statement that you want the connection pool to run whenever a connection is created.

Tomcat JDBC Connection Pool

A.

你是我的挚爱i 2025-01-03 09:29:13

一种方法是使用自定义的 Hibernate 方言,因为您实际上指定了一种与数据库对话的“不同”方式。我不知道在哪里添加初始化代码。

One way to go would be to use a custom Hibernate dialect, since you are actually specifying a 'different' way to talk with the database. I have no idea where to add the initialization code though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文