对象序列化和持久化.ppt

上传人:sccc 文档编号:5117278 上传时间:2023-06-05 格式:PPT 页数:95 大小:1.69MB
返回 下载 相关 举报
对象序列化和持久化.ppt_第1页
第1页 / 共95页
对象序列化和持久化.ppt_第2页
第2页 / 共95页
对象序列化和持久化.ppt_第3页
第3页 / 共95页
对象序列化和持久化.ppt_第4页
第4页 / 共95页
对象序列化和持久化.ppt_第5页
第5页 / 共95页
点击查看更多>>
资源描述

《对象序列化和持久化.ppt》由会员分享,可在线阅读,更多相关《对象序列化和持久化.ppt(95页珍藏版)》请在三一办公上搜索。

1、对象序列化和持久化,Object Serialization and Persistence,2023/6/5,Institute of Computer SoftwareNanjing University,1,摘要,对象序列化对象持久化Language levelDatabasesHibernate,2023/6/5,Institute of Computer SoftwareNanjing University,2,摘要,对象序列化对象持久化Language levelDatabasesHibernate,2023/6/5,Institute of Computer SoftwareNa

2、njing University,3,摘要,对象序列化对象持久化Language levelDatabasesHibernate,2023/6/5,Institute of Computer SoftwareNanjing University,4,Object Serialization,WhyWhatHow,2023/6/5,Institute of Computer SoftwareNanjing University,5,Java Object Serialization-Why,Serialization is used for lightweight persistence and

3、 for communication via sockets or Remote Method Invocation(RMI).,2023/6/5,Institute of Computer SoftwareNanjing University,6,Java Object Serialization-Example,public class Client public static void main(String args)try/Create a socket Socket soc=new Socket(InetAddress.getLocalHost(),8020);OutputStre

4、am o=soc.getOutputStream();ObjectOutput s=new ObjectOutputStream(o);s.writeObject(Todays date);s.writeObject(new Date();s.flush();s.close();catch(Exception e)System.out.println(e.getMessage();System.out.println(Error during serialization);System.exit(1);,2023/6/5,Institute of Computer SoftwareNanjin

5、g University,7,Java Object Serialization-Example,public class Server public static void main(String args)ServerSocket ser=null;Socket soc=null;String str=null;Date d=null;try ser=new ServerSocket(8020);soc=ser.accept();InputStream o=soc.getInputStream();ObjectInput s=new ObjectInputStream(o);str=(St

6、ring)s.readObject();d=(Date)s.readObject();s.close();System.out.println(str);System.out.println(d);catch(Exception e)System.out.println(e.getMessage();System.out.println(Error during serialization);System.exit(1);,2023/6/5,Institute of Computer SoftwareNanjing University,8,Java Object Serialization-

7、Example,Writing to an object stream,2023/6/5,Institute of Computer SoftwareNanjing University,9,/Serialize todays date to a file.FileOutputStream f=new FileOutputStream(tmp);ObjectOutput s=new ObjectOutputStream(f);s.writeObject(Today);s.writeObject(new Date();s.flush();,Java Object Serialization-Ex

8、ample,Reading from an object stream,2023/6/5,Institute of Computer SoftwareNanjing University,10,/Deserialize a string and date from a file.FileInputStream in=new FileInputStream(tmp);ObjectInputStream s=new ObjectInputStream(in);String today=(String)s.readObject();Date date=(Date)s.readObject();,Ja

9、va Object Serialization-What,Object Serialization extends the core Java Input/Output classes with support for objects.Object Serialization supports the encoding of objects,and the objects reachable from them,into a stream of bytes;and it supports the complementary reconstruction of the object graph

10、from the stream.,2023/6/5,Institute of Computer SoftwareNanjing University,11,Java Object Serialization-Goal,Have a simple yet extensible mechanism.Maintain the Java object type and safety properties in the serialized form.Be extensible to support marshaling and unmarshaling as needed for remote obj

11、ects.Be extensible to support simple persistence of Java objects.Require per class implementation only for customization.Allow the object to define its external format.,2023/6/5,Institute of Computer SoftwareNanjing University,12,Java Object Serialization-How,Objects to be saved in the stream may su

12、pport either the Serializable or the Externalizable interface.For Serializable objects,the stream includes sufficient information to restore the fields in the stream to a compatible version of the class.For Externalizable objects,the class is solely responsible for the external format of its content

13、s.,2023/6/5,Institute of Computer SoftwareNanjing University,13,The Serializable Interface,public interface java.io.Serializable;A Serializable class must do the following:Implement the java.io.Serializable interface Identify the fields that should be serializableHave access to the no-arg constructo

14、r of its first nonserializable superclass,2023/6/5,Institute of Computer SoftwareNanjing University,14,The Serializable Interface,The class can optionally define the following methods:writeObject(ObjectOutputStream)readObject(ObjectInputStream)writeReplace()readResolve(),2023/6/5,Institute of Comput

15、er SoftwareNanjing University,15,思考:如果一个可序列化的类实现了以上四个方法,那么在序列化和反序列化的过程中,这几个方法的调用次序如何?,The Externalizable Interface,public interface Externalizable extends Serializable public void writeExternal(ObjectOutput out)throws IOException;public void readExternal(ObjectInput in)throws IOException,java.lang.C

16、lassNotFoundException;,2023/6/5,Institute of Computer SoftwareNanjing University,16,The Externalizable Interface,The class of an Externalizable object must do the following:Implement the java.io.Externalizable interface Implement a writeExternal method to save the state of the objectImplement a read

17、External method to read the data written by the writeExternal method from the stream and restore the state of the object Have the writeExternal and readExternal methods be solely responsible for the format,if an externally defined format is written Have a public no-arg constructor,2023/6/5,Institute

18、 of Computer SoftwareNanjing University,17,The Externalizable Interface,An Externalizable class can optionally define the following methods:writeReplacereadResolve,2023/6/5,Institute of Computer SoftwareNanjing University,18,Note:声明类实现Externalizable接口会有重大的安全风险。writeExternal()与readExternal()方法声明为publ

19、ic,恶意类可以用这些方法读取和写入对象数据。如果对象包含敏感信息,则要格外小心。,区别,Serializable自动存储必要信息,用以反序列化被存储的实例优点内建支持易于实现缺点占用空间过大速度慢,Externalizable只保存被存储的类的标识,完全由程序员完成读取和写入工作优点开销较少可能的速度提升缺点虚拟机不提供帮助,程序员负担重,2023/6/5,19,Institute of Computer SoftwareNanjing University,serialVersionUID,private static final long serialVersionUIDFor comp

20、abilityInvalidClassException It is strongly recommended that all serializable classes explicitly declare serialVersionUID valuesserialver;eclipse,2023/6/5,Institute of Computer SoftwareNanjing University,20,Serialization Principles,如果该类有父类如果父类实现了可序列化接口,则OK如果父类没有实现可序列化接口,则父类所有字段的属性默认情况下不会被序列化如果该类的某个属

21、性标识为static类型的,则该属性不能序列化;如果该类的某个属性采用transient关键字标识,则该属性不能序列化;,2023/6/5,Institute of Computer SoftwareNanjing University,21,Serialization Principles,在我们标注一个类可以序列化的时候,其以下属性应该设置为transient来避免序列化:线程相关的属性;需要访问IO、本地资源、网络资源等的属性;没有实现可序列化接口的属性;,2023/6/5,Institute of Computer SoftwareNanjing University,22,Some

22、Items from Effective Java,2023/6/5,Institute of Computer SoftwareNanjing University,23,Effective Java for Serialization,1.Implement Serializable judiciously 谨慎地实现Serializable代价1:一旦一个类被发布,则“改变这个类的实现”的灵活性将大大降低。序列化会使类的演化受到限制。代价2:增加了错误和安全漏洞的可能性。序列化机制是一种语言之外的对象创建机制。代价3:随着一个类的新版本的发行,相关的测试负担增加了。可序列化类的变化越大,

23、它就越需要测试。,2023/6/5,Institute of Computer SoftwareNanjing University,24,Effective Java for Serialization,Notes:为了继承而设计的类应该很少实现Serializable,接口也应该很少会扩展它。对于为继承而设计的不可序列化的类,应该考虑提供一个无参数的构造函数。内部类应该很少实现Serializable。,2023/6/5,Institute of Computer SoftwareNanjing University,25,Effective Java for Serialization,

24、2.Consider using a custom serialized form 考虑使用自定义的序列化形式如果一个对象的物理表示等同于它的逻辑内容,则默认的序列化形式可能是合适的。即使确定了默认序列化形式是合适的,通常仍然要提供一个readObject方法以保证约束关系和安全性。,2023/6/5,Institute of Computer SoftwareNanjing University,26,Effective Java for Serialization,2023/6/5,Institute of Computer SoftwareNanjing University,27,Ef

25、fective Java for Serialization,2023/6/5,Institute of Computer SoftwareNanjing University,28,Effective Java for Serialization,当一个对象的物理表示与它的逻辑数据内容有实质性的区别时,使用默认序列化形式有4个缺点:它使这个类的导出API永久地束缚在该类的内部表示上。它要消耗过多的空间。它要消耗过多的时间。它会引起栈溢出。,2023/6/5,Institute of Computer SoftwareNanjing University,29,2023/6/5,Institu

26、te of Computer SoftwareNanjing University,30,Effective Java for Serialization,2023/6/5,Institute of Computer SoftwareNanjing University,31,Effective Java for Serialization,如果所有的实例域都是transient的,那么省去调用defaultWriteObject和defaultReadObject也是允许的,但是不推荐这样做。在决定将一个域做成非transient之前,请一定要确信它的值将是该对象逻辑状态的一部分。不管你选择

27、了哪种序列化形式,你都要为自己编写的每个序列化的类声明一个显式的序列化版本UID。,2023/6/5,Institute of Computer SoftwareNanjing University,32,private static final long serialVersionID=randomLongValue,Effective Java for Serialization,3.Write readObject methods defensively 保护性地编写readObject方法readObject方法实际上相当于另一个共有的构造函数,如同其他构造函数一样,它也要求所有同样的

28、注意事项:检查实参的有效性,并且必要时对参数进行保护性拷贝。,2023/6/5,Institute of Computer SoftwareNanjing University,33,Versioning,Versioning raises some fundamental questions about the identity of a class,including what constitutes a compatible change.A compatible change is a change that does not affect the contract between t

29、he class and its callers.,2023/6/5,Institute of Computer SoftwareNanjing University,34,Incompatible changes,Deleting fieldsMoving classes up or down the hierarchyChanging a nonstatic field to static or a nontransient field to transientChanging the declared type of a primitive fieldChanging the write

30、Object or readObject method so that it no longer writes or reads the default field data or changing it so that it attempts to write it or read it when the previous version did not.,2023/6/5,Institute of Computer SoftwareNanjing University,35,Incompatible changes,Changing a class from Serializable to

31、 Externalizable or vice versaChanging a class from a non-enum type to an enum type or vice versaRemoving either Serializable or ExternalizableAdding the writeReplace or readResolve method to a class is incompatible if the behavior would produce an object that is incompatible with any older version o

32、f the class.,2023/6/5,Institute of Computer SoftwareNanjing University,36,Compatible changes,Adding fieldsAdding classesRemoving classesAdding writeObject/readObject methodsRemoving writeObject/readObject methodsAdding java.io.SerializableChanging the access to a fieldChanging a field from static to

33、 nonstatic or transient to nontransient,2023/6/5,Institute of Computer SoftwareNanjing University,37,摘要,对象序列化对象持久化Language levelDatabasesHibernate,2023/6/5,Institute of Computer SoftwareNanjing University,38,摘要,对象序列化对象持久化Language levelDatabasesHibernate,2023/6/5,Institute of Computer SoftwareNanjing

34、 University,39,Object Persistence,During execution of application:objects are created and manipulatedWhat happens to objects after termination?Various kinds of objectsTransient objects:Disappear with current sessionPersistent objects:Stay around from session to sessionMay be shared with other applic

35、ations(e.g.databases),2023/6/5,Institute of Computer SoftwareNanjing University,40,Approaches to manipulate persistent objects,Persistence mechanisms from programming languagesRelational databasesObject-oriented databases,2023/6/5,Institute of Computer SoftwareNanjing University,41,Persistence from

36、programming languages,Mechanisms for storing objects in files and retrieving themSimple objects:e.g.integers,charactersconventional methods usableComposite objects:contain references to other objectsPersistence Closure principle:Any storage and retrieval mechanism must handle the object and all its

37、dependents.otherwise:dangling references,2023/6/5,Institute of Computer SoftwareNanjing University,42,对象结构的存储与提取,对象持久化的难点之一:对象之间的引用,2023/6/5,Institute of Computer SoftwareNanjing University,43,对象结构的存储与提取,需持久化整个对象引用闭包Persistence closureJava的serialization规则缺省规则:非static 非transient 的数据成员用户定义class List i

38、mplements Serializable List next;private static final ObjectStreamField serialPersistentFields=new ObjectStreamField(next,List.class);,2023/6/5,Institute of Computer SoftwareNanjing University,44,对象结构的存储与提取,闭包可能太大小对象引用(共享的)大对象,2023/6/5,Institute of Computer SoftwareNanjing University,45,对象结构的存储与提取,J

39、ava 的 transient 修饰子Transient fields 不被序列化Static fields 也不被序列化开发者负责维护,2023/6/5,Institute of Computer SoftwareNanjing University,46,Schema evolution,Fact:Classes changeProblem:Objects are stored of which class descriptions have changedSchema evolution:At least one class used by the retrieving system d

40、iffers from its counterpart stored by the storing system.Object retrieval mismatch(Object mismatch):The retrieving system retrieves a particular object whose own generating class was different in the storing system.No fully satisfactory solution,2023/6/5,Institute of Computer SoftwareNanjing Univers

41、ity,47,Different approaches,Naive,extreme approaches:Forsake previously stored objectsOver a migration path from old format to newa one-time,en masse conversion of old objectsnot applicable to a large persistent store or to one that must be available continuouslyMost general solution:On-the-fly conv

42、ersionNote:We cover only the retrieval part.Whether to write back the converted object is a separate issue.,2023/6/5,Institute of Computer SoftwareNanjing University,48,On-the-fly object conversion,Three separate issues:Detection:Catch object mismatchNotification:Make retrieving system aware of obje

43、ct mismatchCorrection:Bring mismatched object to a consistent stateMake it a correct instance of the new class version,2023/6/5,Institute of Computer SoftwareNanjing University,49,Detection,Detect a mismatch between two versions of an objects generating classTwo categories of detection policy:Nomina

44、l approach:Each class version has a version nameCentral registration mechanism necessaryStructural approach:Deduce class descriptor from actual class structureStore class descriptorSimple detection:compare class descriptors of retrieved object with new class descriptor,2023/6/5,Institute of Computer

45、 SoftwareNanjing University,50,Detection:Structural Approach,What does the class descriptor need to contain?Trade-off between efficiency and reliabilityTwo extreme approaches:C1:class nameC2:entire class text(e.g.abstract syntax tree)Reasonable approaches:C3:class name,list of attributes(name and ty

46、pe)C4:in addition to C3:class invariant,2023/6/5,Institute of Computer SoftwareNanjing University,51,Notification,What happens when the detection mechanism has caught an object mismatch?Language level mechanism Class ANY could include a procedure:correct_mismatch is-Handle object retrieval mismatch.

47、localexception:EXCEPTIONSdocreate exceptionexception.raise(Routine failure:Object mismatch during retrieval)end,2023/6/5,Institute of Computer SoftwareNanjing University,52,Correction,How do we correct an object that caused a mismatch?Current situation:Retrieval mechanism has created a new object(de

48、duced from a stored object with same generating class)A mismatch has been detected new object is in temporary(maybe inconsistent)state,2023/6/5,Institute of Computer SoftwareNanjing University,53,Correction,增加attribute删除attribute,2023/6/5,Institute of Computer SoftwareNanjing University,54,0.0,Attri

49、bute was not in stored version.Field is initialized to default value of attribute type,Stored version had a field.New version has removed attribute.,Attributes have not changed.,Correction,correct_mismatch is-Handle object retrieval mismatch-by correctly setting up balance.dobalance:=deposits.total-

50、withdrawals.totalensureconsistent:balance=deposits.total-withdrawals.totalend,2023/6/5,Institute of Computer SoftwareNanjing University,55,deposits,withdrawals,balance 0.0,900,100,200,240,300,new field(initialized to default value),old fields,Wrong!,维护不变式,自动对象转换:Java,serialVersionUID 自动定义(根据类文件生成)1.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号