用于 postgresql 的 Scala Lift 数据库连接

发布于 2024-12-22 16:35:30 字数 844 浏览 2 评论 0原文

我正在做 Timothy Perrett 的《Lift In Action》,这本书在数据库连接方面有点令人困惑。

这是链接: https ://github.com/timperrett/lift-in-action/blob/master/chapter-3/src/main/scala/bootstrap/liftweb/Boot.scala

令人困惑的部分是JNDI。我没有 java 背景,所以有点混乱,而且 prop 文件也很混乱。它有点类似于配置文件或 ini 文件,但仍然有很多我不明白的地方。

我找到了另一个教程: http://exploring.liftweb.net/master/index-8.html @ 8.1.2 设置数据库连接

这有点不同。对于有 PHP 背景的我来说,这是更熟悉的。

那么有什么区别呢?这两种不同的方法有什么优点吗?而且第二种方法不会检查连接是否未建立,我该如何检查?或者你们可以提供任何代码片段来向我展示故障转移的最佳实践吗?

哦! Load Shark 模式用于确保只使用一个数据库连接,对吗?

感谢您抽出时间。

编辑: 我相信连接是否建立的检查是在 DBVendor 对象的 newConnection 上已经存在的数据库对象中进行的。我说得对吗?

I'm doing the Lift In Action by Timothy Perrett and the book is a bit confusing on db connection.

Here's the link:
https://github.com/timperrett/lift-in-action/blob/master/chapter-3/src/main/scala/bootstrap/liftweb/Boot.scala

The confusing part, is JNDI. I do not have a java background so it's a bit confusing and the prop files are confusing too. It's a bit similar the config files or ini files but still there's a lot going on that I don't understand.

I've found another tutorial:
http://exploring.liftweb.net/master/index-8.html
@ 8.1.2 Setting Up the Database Connection

This is a bit different. Which is more familiar to me coming from a PHP background.

So what's the difference? And is there any advantages between the two different methods? And also the second method doesn't check for if the connection isn't establish, how do I check for it? Or is there any snippet of code you guys can provide to show me the best practice of fail over?

oh! The load shark pattern is used to make sure that only one db connection is use right?

Thank you for your time.

edit:
I believe the check for connection is establish or not is in the database object already on newConnection in the DBVendor object. Am I correct?

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

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

发布评论

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

评论(1

滥情空心 2024-12-29 16:35:30

tl;dr

将你的数据库配置放在./src/main/resources/default.props中,格式为:

db.class=org.postgresql.Driver
db.url=jdbc:postgresql:[//host[:port]]/database
db.user=user
db.pass=password

当然,你需要为你的数据库提供正确的值。

长话短说

如果 JNDI 令人困惑,您可以暂时忽略它。它是 Java EE 的一部分,如果您正在尝试,它会很有用配置生产应用程序以在应用程序服务器上运行,但这不是必需的。

属性是Java中处理配置的一种古老方法。属性文件(通常名为 .props.properties)只是名称/值对的列表。

Lift 提供了 Props 对象,方便为不同的环境提供不同的属性文件。 wiki 对它的工作原理有很好的描述,但基本上你可以为运行模式(测试、登台、生产等)、主机名和用户名的任意组合指定不同的属性。

现在,分解 Tim Perrett 的代码:

  1. 检查 JNDI 连接是否可用。除非您运行应用程序服务器并进行设置,否则不会。如果不是的话也没什么大不了的。
  2. 如果没有 JNDI,它将使用Database 对象来配置数据源。
  3. Database 对象使用 Lift 的 Props 对象从与您所运行的系统最匹配的 .props 文件中加载值。如果您仅提供 default.props,则这些是将使用的值。
  4. 如果没有属性文件,或者它不包含数据库配置键,则 openOr 提供使用 H2 数据库

Lift 已经进行了自己的连接管理,这通常意味着每个线程都有不同的数据库连接。如果无法建立连接,您将从数据访问代码中收到SQLException

tl;dr

Put your database configuration in ./src/main/resources/default.props in the form of:

db.class=org.postgresql.Driver
db.url=jdbc:postgresql:[//host[:port]]/database
db.user=user
db.pass=password

Of course, you need to provide the correct values for your database.

the long story

If JNDI is confusing, you can ignore it for now. It is a part of Java EE which is useful if you're trying to configure production applications to run on an application server, but not necessary.

Properties are an age-old method in Java for handling configuration. A properties file (which is usually named .props or .properties) is just a list of name/value pairs.

Lift provides a Props object for conveniently providing different properties files for different environments. The wiki has a good write-up for how it works, but basically you can specify different properties for any combination of run mode (test, staging, production, etc.), hostname and username.

Now, breaking down Tim Perrett's code:

  1. Check whether a JNDI connection is available. Unless you run an app server and set that up, it won't be. Not a big deal if it's not.
  2. If there's no JNDI, it uses the Database object to configure the data source.
  3. The Database object uses Lift's Props object to load the values from the whichever .props file best matches the system you're running on. If you only provide default.props, those are the values that will be used.
  4. If there is no properties file, or it doesn't contain database config keys, then the openOr provides default values for using an H2 database.

Lift already does its own connection management, which typically means a different database connection per-thread. If a connection cannot be established, you'll get SQLException from your data access code.

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