简体   繁体   English

在两个微服务之间共享单个数据库连接

[英]Share single database connection between two micro-services

I have two micro-services written using .net core that shares a single database (sql server) i want to unify database connection between the two micro-services is this possible to do?我有两个使用 .net 内核编写的微服务,它们共享一个数据库(sql server)我想统一两个微服务之间的数据库连接这可能吗?

.Net and database server instance manage Actual(physical) database connections. .Net 和数据库服务器实例管理实际(物理)数据库连接。 Every time applications request to open a connection to the database;每次应用程序请求打开与数据库的连接时; it is provided from a connection pool.它是从连接池提供的。 And, when a connection is closed in an application, it is returned to the pool.而且,当应用程序中的连接关闭时,它会返回到池中。
You may able to share the connection object by some of the inter-process-communication methods and native coding, but it is not a good pattern.您可以通过一些进程间通信方法和本机编码共享连接 object,但这不是一个好的模式。
If you need a connection for keeping a transaction in both services, it is better to do that by using distributed transactions.如果您需要一个连接来保持两个服务中的事务,最好使用分布式事务来做到这一点。

Can you explain with more detail what you want to do or what problem you are having?您能否更详细地解释您想做什么或遇到什么问题?

You can create 2 different users for each of the 2 micro-services so you can manage their premissions separately.您可以为 2 个微服务中的每一个创建 2 个不同的用户,以便您可以分别管理他们的权限。
That's the recommended option if they use different tables/databases or they do different things (One can only read a single table, the other one can read and write to a different table).如果他们使用不同的表/数据库或者他们做不同的事情(一个只能读取一个表,另一个可以读取和写入不同的表),这是推荐的选项。

If you don't need that then you can use a single user for both micro-services.如果您不需要,那么您可以将单个用户用于两个微服务。 The user has a "Max Connections" property for that.用户有一个“最大连接数”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM