两个应用程序实例连接到相同的、修改过的数据库

时间:2022-10-03 23:30:52

, Hello,

,你好,

couple days ago at interview i was asked a strange(for me) question:

几天前的面试中,有人问了我一个奇怪的问题:

Imagine that you have 2 nodes with 1 instance of application in java per each. Each of that instances use same relational database - at node 3. Now you need to make update for that database with new schema but you need to upgrade only 1 node.

假设您有两个节点,每个节点都有一个java应用程序实例。每个实例都使用相同的关系数据库——节点3。现在需要使用新的模式对该数据库进行更新,但只需要升级一个节点。

So you have 1 instances of old application with old database config and 1 instances of new application with new database config running at same time with connection to same database.

因此,您有1个旧应用程序的实例和1个新应用程序的实例,新应用程序的新数据库配置同时运行,并连接到相同的数据库。

And after some time if update is working fine old instance will be updated to new versions.

一段时间后,如果更新工作正常,旧实例将被更新为新版本。

I told them that i don't know how to do that - as for i know it's impossible to run two versions of application that use different versions of database (some change was made to columns can't remember exactly).

我告诉他们我不知道怎么做——因为我知道不可能运行两个使用不同版本的数据库的应用程序(对列做了一些更改,但不能准确地记住)。

Now after couple of days i still cannot find answer to that question. I have googled for answer but i have found nothing.

过了几天,我仍然找不到这个问题的答案。我在谷歌上搜索了答案,但一无所获。

Could you please tell me if that's possible and give me some keywords to look for or links to tutorials or articles ?

你能告诉我这是否有可能,并给我一些关键字来查找或链接到教程或文章吗?

Regards

问候

1 个解决方案

#1


2  

Don't make changes that break things. (You already know this part.)

不要做会破坏事情的改变。(你已经知道这部分了。)

Adding a new, nullable column shouldn't break old code. Adding a new, nonnullable column will break old code. But use one or more triggers to manage the default value, and old code will keep working. These kinds of triggers are usually deprecated and dropped later.

添加一个新的可空列不应该破坏旧代码。添加一个新的不可空列将破坏旧代码。但是使用一个或多个触发器来管理默认值,旧代码将继续工作。这些类型的触发器通常被弃用,稍后再删除。

Dropping a column will break old and new code. So deprecate the column, change the code to stop using the column, then drop it.

删除一个列将破坏旧代码和新代码。因此,弃用该列,更改代码以停止使用该列,然后删除它。

The underlying principles are simple. (But simple != easy.) There's more than one way to do this, and details vary.

基本原则很简单。(但简单! =容易。)这样做的方法不止一种,细节也各不相同。

  • If you don't know for certain that a change won't break things, treat it like a breaking change.

    如果你不确定改变不会破坏事情,那么就把它当成一个彻底的改变。

  • Identify the things that have to appear unchanged for the old code.

    识别必须为旧代码保持不变的东西。

  • Alter the database to keep working those things you just identified. That might involve adding triggers, updatable views, stored procedures, and so on.

    修改数据库,使其继续运行刚才标识的内容。这可能涉及添加触发器、可更新视图、存储过程等。

  • Write this stuff down. You're going to use it a lot.

    把这些东西写下来。你会经常用到它。

  • Update the code.

    更新代码。

  • Deprecate and remove the things you created to keep things working.

    弃用并删除创建以保持工作的内容。

You ought to be able to catch these kinds of issues in your test environment. ("All of us have a test environment. Some of us are lucky enough to have a separate production environment." -- Unknown)

您应该能够在测试环境中捕获这些问题。我们都有一个测试环境。我们中的一些人很幸运,拥有一个独立的生产环境。——未知)

#1


2  

Don't make changes that break things. (You already know this part.)

不要做会破坏事情的改变。(你已经知道这部分了。)

Adding a new, nullable column shouldn't break old code. Adding a new, nonnullable column will break old code. But use one or more triggers to manage the default value, and old code will keep working. These kinds of triggers are usually deprecated and dropped later.

添加一个新的可空列不应该破坏旧代码。添加一个新的不可空列将破坏旧代码。但是使用一个或多个触发器来管理默认值,旧代码将继续工作。这些类型的触发器通常被弃用,稍后再删除。

Dropping a column will break old and new code. So deprecate the column, change the code to stop using the column, then drop it.

删除一个列将破坏旧代码和新代码。因此,弃用该列,更改代码以停止使用该列,然后删除它。

The underlying principles are simple. (But simple != easy.) There's more than one way to do this, and details vary.

基本原则很简单。(但简单! =容易。)这样做的方法不止一种,细节也各不相同。

  • If you don't know for certain that a change won't break things, treat it like a breaking change.

    如果你不确定改变不会破坏事情,那么就把它当成一个彻底的改变。

  • Identify the things that have to appear unchanged for the old code.

    识别必须为旧代码保持不变的东西。

  • Alter the database to keep working those things you just identified. That might involve adding triggers, updatable views, stored procedures, and so on.

    修改数据库,使其继续运行刚才标识的内容。这可能涉及添加触发器、可更新视图、存储过程等。

  • Write this stuff down. You're going to use it a lot.

    把这些东西写下来。你会经常用到它。

  • Update the code.

    更新代码。

  • Deprecate and remove the things you created to keep things working.

    弃用并删除创建以保持工作的内容。

You ought to be able to catch these kinds of issues in your test environment. ("All of us have a test environment. Some of us are lucky enough to have a separate production environment." -- Unknown)

您应该能够在测试环境中捕获这些问题。我们都有一个测试环境。我们中的一些人很幸运,拥有一个独立的生产环境。——未知)