Persistence (persistence) is the saving of data (such as in-memory objects) to a storage device that can be permanently saved (such as a disk). The primary application of persistence is to store in-memory objects in relational databases, and of course, in disk files, XML data files, and so on. Persistence is the mechanism by which program data is transformed between a persistent state and a transient state. JDBC is a kind of persistence mechanism. File IO is also a persistence mechanism. Persistence is an object service that saves in-memory objects to external memory, allowing them to be retrieved later. You need to implement at least 3 interfaces: void Save (Object o) to save an object to external memory object Load (Object OID) to retrieve objects from external memory by object ID boolexists (Object oid) Check if an object exists in external memory why do I need to persist the service? That is due to a flaw in the memory itself:Serialization ofLet's jump off and look at another useful concept like: Serialization is also an object service that serializes objects in memory into streams, or deserializes streams into objects. You need to implement 2 interfaces: void Serialize (Stream stream,object O) Serializes the object into the stream, object deserialize (stream stream) deserializes the stream into object serialization and persistence very similar, Some people even confuse, in fact, there is a difference, serialization is to solve the problem of transmission of objects, transmission can be between threads, processes, memory external memory between, between the host. The reason I mention serialization here is that we can use serialization to aid persistence, and that any object that can be persisted can be serialized, because serialization is relatively easy (and not easy), so the mainstream software infrastructure, such as. NET and Java, has already completed the serialized framework. The persistence scheme can be divided into relational database scheme, file scheme, object database scheme, XML database scheme, the current mainstream persistence scheme is the relational database scheme, the relational database scheme not only solves the problem of concurrency, but more importantly, the relational database also provides the value beyond the Persistence service: statistical analysis function. As I said just now, anything that can be serialized can be persisted, and at the extreme, we can just build a Table object (oid,bytes), but basically nobody does it, because once that happens, we lose the additional statistical analysis function of the relational database. There is a gap between the relational database and the object-oriented, because the pattern in 2 does not match, so there is an or mapping problem. What is "persistence"
Persistent (persistence), which is to save data (such as in-memory objects) to a storage device that can be permanently saved (such as a disk). The primary application of persistence is to store in-memory data in a relational database, and of course it can be stored in disk files, XML data files, and so on.
To talk about persistence (persistence) and serialization (serialization), I used to confuse them, but the difference between the two is really not big, and the similarities are all attempts to convert objects and other things, The difference is that persistence is the desire to persist it and get it back when needed, and serialization is concerned with how to turn an object into a byte stream, essentially from a practical point of view, the two have a large degree of mutual coverage, the serialization mechanism is to convert the value of the class into a general (that is, continuous) byte stream, The stream is then written to a process on a disk file or any other streaming target, which in itself can be called persistence. Of course, the so-called deserialization, as the name implies is to reverse the serialization process, from the flow target to get the process of the object.
The process of serialization is that the object writes the byte stream and reads the object from the byte stream. After the object state is converted to a byte stream,
You can use the various byte stream classes in the Java.io package to save it to a file, pipe to another thread, or connect over a network
Sends the object data to another host. Object serialization is simple and powerful, in RMI, Socket, JMS, EJB
have been applied. Object serialization problem is not the most exciting topic in network programming, but it is very important, it has
A lot of practical significance.
One: Object serialization can implement distributed objects. Main applications For example: RMI to use object serialization to run a service on a remote host, just as you would when running an object on a local machine.
Two: Java object serialization preserves not only the data of an object, but also the data of each object referenced by the object. You can write the entire object hierarchy to a stream of bytes that can be saved in a file or passed on a network connection. Object sequences can be used to "deep copy" objects, that is, to copy the object itself and the referenced object itself. Serializing an object may get the entire sequence of objects.
From the above narrative, we know that object serialization is an essential weapon in Java programming, so let's start with the basics and learn about its mechanics and usage.
Java serialization is relatively straightforward and typically does not require writing custom code to save and restore object state. The class object of the real Java.io.Serializable interface can be converted into a byte stream or recovered from a byte stream, without adding any code to the class. Only rare cases require custom code to save or Restore object state. Note here that not every class can be serialized, and some classes cannot be serialized, such as a thread-related class with a very complex relationship to a particular JVM.
Serialization mechanism:
Serialization consists of two parts: serialization and deserialization. Serialization is the first part of this process that decomposes data into a byte stream for storage in a file or on a network. Deserialization is the opening of a byte stream and refactoring the object. Object serialization not only converts the base data type to a byte representation, but sometimes restores the data. Recovering data requires an object instance that has recovery data. The serialization process in ObjectOutputStream is connected to a byte stream, including object type and version information. When deserializing, the JVM generates an object instance with header information and then copies the data in the object stream into the object data member.
Here we have two parts to illustrate:
To process the object flow:
(Serialization process and deserialization process)
The Java.io package has a class of two serialized objects. The ObjectOutputStream is responsible for writing the object to the byte stream, ObjectInputStream the object from the byte stream.
Let's get to know the ObjectOutputStream class first. The ObjectOutputStream class extends the DataOutput interface. The WriteObject () method is the most important method for object serialization. If the object contains references to other objects, the WriteObject () method recursively serializes the objects. Each ObjectOutputStream maintains a serialized object reference table, preventing multiple copies of the same object from being sent. (This is important) because WriteObject () can serialize an entire set of cross-referenced objects, so the same ObjectOutputStream instance may be accidentally requested to serialize the same object. At this point, the deserialization is serialized instead of being written to the object byte stream again.
Let's take a look at the ObjectOutputStream class from the examples below.
Now, let's get to know the ObjectInputStream class. It is similar to ObjectOutputStream. It extends the Datainput interface. The method in the ObjectInputStream image DataInputStream reads the public method of the Java base data type. The ReadObject () method deserializes an object from a byte stream. Each call to the ReadObject () method returns the next object in the stream. The object byte stream does not transmit the class's bytecode, but includes the class name and its signature. ReadObject () When an object is received, the JVM loads the class specified in the header. If this class is not found, then ReadObject () throws ClassNotFoundException, and if you need to transfer object data and bytecode, you can use the RMI framework. The remaining methods of ObjectInputStream are used to customize the deserialization process.
Examples are as follows:
Deserializes a string object and a Date object fileinputstream in = new FileInputStream ("TMP") from a file; ObjectInputStream s = new ObjectInputStream (in); String today = (string) s.readobject (); Date date = (date) s.readobject ();
To customize the serialization process:
Serialization can usually be done automatically, but this process can sometimes be controlled. Java can declare a class as serializable, but it can still manually control data members that are declared static or transient.
Example: A very simple serialization class. The public class Simpleserializableclass fully customizes the serialization process:
If a class is fully responsible for its own serialization, implement the Externalizable interface instead of the serializable interface. The Externalizable interface definition consists of two methods writeexternal () and readexternal (). These methods allow you to control how the object data members write to the byte stream. When the class implements Externalizable, the header is written to the object stream, and the class is fully responsible for serializing and recovering the data members, except for the headers, which are not automatically serialized at all. You have to pay attention here. Declaring a class to implement a externalizable interface can have significant security risks. The Writeexternal () and Readexternal () methods are declared public, and malicious classes can use these methods to read and write object data. If the object contains sensitive information, take extra care. This includes using Secure Sockets or encrypting the entire byte stream.
The difference between serialization and persistence