Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 学习心得/方法/资源  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于对象内的数据和数据库中的数据保持同步的一个问题!
kakaluote





发贴: 1
积分: 0
于 2008-06-25 10:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
先看如下代码:


Class A
{
private String name; //名称
private DbOper oper=new DbOper(); //操作数据库的类

//改变名称
public void setName(String name)
{
this.name=name; //改变对象中的属性
oper.changName(name); //同时改变数据库中的数据
}
}


我的问题是当我更新了对象中的属性以后,要是数据库执行失败了,那么对象中的属性已经改变,而数据库中的数据还没有更改,这样数据不是不同步了吗?请问大家都是怎么处理这种情况的。


HenryShanley edited on 2008-06-25 16:13

作者 Re:关于对象内的数据和数据库中的数据保持同步的一个问题! [Re:kakaluote]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-06-25 16:30 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The magic behind this question is some mysterious guy called "transaction".

In software, all-or-nothing operations are called transactions. Transactions allow you to group several operations into a single unit of work that either fully happens or fully doesn't happen. If everything foes well, then the transaction is a success. But if anything goes wrong, the slate is wiped clean and it is as if nothing ever happened.

You have given a perfect example of a real-world transaction. If the database name changing fails, the person could think about he/she has changed his/her name but actually not. There a re various ways declaring transactions and because you have hidden your database implementation behind the scheme, so I don't know which database approach you are using. (e.g. JDBC, Hibernate ORM, or something else).

You can even think about let your database implementation throw an exception when database operation fails. Check the following code snippet.

Class A
{
private String name; //名称
private DbOper oper=new DbOper(); //操作数据库的类

//改变名称
public void setName(String name)
{
try
{
oper.changName(name); //同时改变数据库中的数据
this.name=name; //改变对象中的属性
}
catch (DbException dbexcept)
{
// handles with the database exception
// e.g. log an error message to the end operator...
}
}
}


The above code can guarantee the name property is never set in your object model if the database operation fails, although it looks very simple and fragile.

Hope this helps.

Regards,
Jiafan



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923