VB.net:使用共享方法获取数据库连接有任何问题吗?

发布于 2024-08-02 18:51:03 字数 254 浏览 1 评论 0原文

我有一个实用程序类来创建 &返回数据库连接:

Public Shared Function GetConnection() as OracleConnection
  dim c as New OracleConnection()
  ... set connection string...
  c.Open()
  Return c
End Function

并发调用返回相同连接是否存在风险?连接字符串启用池化。

I have a utility class that creates & returns a db connection:

Public Shared Function GetConnection() as OracleConnection
  dim c as New OracleConnection()
  ... set connection string...
  c.Open()
  Return c
End Function

Is there any risk of concurrent calls returning the same connection? The connection string enables pooling.

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

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

发布评论

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

评论(4

你在看孤独的风景 2024-08-09 18:51:04

我不这么认为。

由于 c 是一个局部变量(“堆栈变量”)而不是静态变量,因此每次调用都有它自己的 c 实例。
接下来,您创建一个新对象(连接)并返回它。

I don't think so.

Since c is a local variable ("stack variable") and not a static one every call has it's own instance of c.
Next you create a new object (the connection) and return this.

不乱于心 2024-08-09 18:51:04

并发性不应该有任何问题,因为每次调用都是一个新连接。

不过,我可能会做出一项更改:将方法设为私有

这将迫使您将所有数据访问代码放在一个类中,并推动创建一个漂亮的、独立的数据访问层。至少将其设置为内部,以便您的数据访问层仅限于单个程序集(与代码的其余部分分开)。

Shouldn't be any problem with concurrency, because each call is a new connection.

I might make one change, though: make the method private.

This will force you to put all your data access code in one class and push to create a nice, separate data access layer. At very least make it internal so that your data access layer is confined to a single assembly (that's separate from the rest of your code).

[浮城] 2024-08-09 18:51:03

由于您每次都会返回一个新连接,因此不会出现任何并发问题。如果您使用 Shared 方法返回对同一个实例的多个引用,这可能是一个问题,但这不是您在这里所做的。

只要每次总是返回数据库连接对象的新实例,就可以安全地以这种方式使用此方法。任何连接池也将像往常一样工作 - 您也无需担心您的 Shared 方法使用在那里产生的问题。

Since you are returning a new connection each time you will not have any concurrency issues. If you were using a Shared method to return multiple references to the same instance, that could be a problem but that is not what you are doing here.

You are safe to use this method in this way as long as you are always returning a new instance of your database connection object each time. Any connection pooling will also work the same as it always would - you won't need to worry about your Shared method usage created problems there either.

诠释孤独 2024-08-09 18:51:03

暂时忘记并发调用问题。如果存在任何连接池,您绝对可以重用相同的底层数据库连接,即使它们不使用相同的对象。

这通常是一件可取的事情,因为打开与数据库的连接可能是一项昂贵的操作。

您是否担心从另一个调用者那里关闭连接对象?如果是这样,正如另一个回复指出的那样,我认为您提供的代码是安全的。

Forget the concurrent calls issue for a moment. If there is any connection pooling going on you will absolutely have reuse of the same underlying database connections, even if they don't use the same object.

This is generally a desirable thing as opening a connection to the DB can be an expensive operation.

Are you worried about closing the connection object out from under another caller? If so, as another response pointed out, I think you are safe with the code you provided.

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