Randomly accessing serialized objects
Is there any way to access objects written to a file randomly. Serialization basically needs accessing objects in the same order as they are writeen. but i need to access object(s) in any order.
Any suggestions on how to do so?
I got this reply when i questioned the person
incharge...may be theres much more to it than i
could specify..........It could also be the case that the person in charge doesn't know that there are databases with small footprints implemented in java.
so i would be glad if soemone
could tell me what ever i plan to do with respect to
random accessing is possible or not.....I think that you should be able to write all objects to one stream, and do a reset (on the object output stream) between each write, and write the start position of each object into an index file.
I'm not sure that it will work, and having a data file + an index file is like having a small database (so why not download a database in that case)
/Kaj
Similar Messages
-
Please Come IN! IanSchneider. About random access serialized objects.
Hi,I'm a freshman.
I have a question about serialized objects.
If I useing writeObject() serialize objects into a file in order,I can gain them with readObject() in the same order. But if I want to random access these serialized objects rather than one by one,how to do?
Mr IanSchneider said "write all your objects into the same file using normal io techniques and you can still generate an index and acheive random access". It seems easy,but how to generate index and use it to acheive random access? Please write something in detail,Thank you in advance!
EXPECTING��Have a look at this class: [ [u]WARNING: I just wrote this code, it hasn't been tested ]
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.ArrayList;
public class SerializedObjectWriter {
private RandomAccessFile raf;
private String filepath;
private String mode;
public SerializedObjectWriter(String filePath, String mode) throws FileNotFoundException {
this.filepath = filePath;
this.mode = mode;
this.raf = new RandomAccessFile(filePath, mode);
public void writeObject(Object o, long pos) throws IOException {
raf.seek(pos);
final byte[] bytes = serialize((Serializable)o);
raf.writeInt(bytes.length);
raf.write(bytes);
public void append(Object o) throws IOException {
writeObject(o, raf.length());
public Object readObject(long pos) throws IOException, ClassNotFoundException {
raf.seek(pos);
int len = raf.readInt();
final byte[] data = new byte[len];
raf.readFully(data);
return deSerialize(data);
public Object[] readAllObjects() throws IOException, ClassNotFoundException {
int pos = 0;
final ArrayList al = new ArrayList();
while (true) {
raf.seek(pos);
final int len = raf.readInt();
final byte[] data = new byte[len];
raf.readFully(data);
al.add(deSerialize(data));
pos = (pos + len + 4);
if (pos >= raf.length()) break;
return al.toArray();
public long length() throws IOException {
return raf.length();
public void reset() throws IOException {
raf.close();
final boolean success = new File(filepath).delete();
if (!success) throw new IOException("Failed to delete file");
raf = new RandomAccessFile(filepath, mode);
public void close() throws IOException {
raf.close();
private byte[] serialize(Serializable obj) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
try {
oos.writeObject(obj);
} finally {
oos.close();
return baos.toByteArray();
private Object deSerialize(byte[] data) throws IOException, ClassNotFoundException {
final ByteArrayInputStream bais = new ByteArrayInputStream(data);
final BufferedInputStream bis = new BufferedInputStream(bais);
final ObjectInputStream ois = new ObjectInputStream(bis);
try {
return (Serializable) ois.readObject();
} finally {
ois.close();
public static void main(String[] args) throws Exception {
SerializedObjectWriter sor = new SerializedObjectWriter("C:\\test.ser", "rw");
sor.reset();
sor.writeObject("Test 1", 0);
sor.append("Test 2");
sor.append("Test 3");
Object[] objects = sor.readAllObjects();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects);
sor.close(); -
Accessing Serialized Files randomly.
I have a problem accessing serailized files randomly. I am using ObjectOutputStream to write objects to a file. I am using the ObjectInputStream to read the objects from the file. Both of these work fine if the order in which i Write objects is the same as the order in which I read Objects. But I need to skip few objects when reading objects from the file. I know API states that the order of writing and reading should be the same. But still is there any way where i can read serailized objects ramdomly form the file.
Please help me with this it is very impritant of the project. We are trying to store tuples in the file. We are going to build an index over these tuples and then read the tuple that we need.
Thankyou,
NishantYou could wrap the ObjectOutputStream around a ByteArrayOutputStream to first serialize an object to a byte[] and then save that into a RandomAccessFile.
The trick is to be able to tell where in the file each serialized object starts. Possibilities include:
1) If your objects are known to serialize to a fixed size (or if there is a reasonable max size of the serialized object), you could pad each byte[] "record" you write to the file (eg, each object always uses 1K bytes). Then you can easily read back the i'th object byte[] and deserialize it.
2) Design into your RandomAccessFile a "directory" section that keeps track of each stored object and it's starting byte address in the file.
If the file needs to hold an arbitrary # of objects, the directory could support a special entry pointing to a "next" directory section later in the file.
-Brian -
Direct DB access from Session Bean w/o using Serialized Objects
I am developing a system where I am receiving some messages (data ) inside session bean and I want to log that data into data base �.i.e inserting that data in to various tables. I am not showing that data to client ( that is taken care by another application).
So I am directly calling insert methods on various tables instead of going for serialized classes for each of that tables and calling setter methods. Is this approach correct? Or this will create nightmares when millions of messages are to be logged? Do I have to make serialized objects? Please post the suggestions ..Thank you in advance.
If session bean is making direct inserts in the DB using Helper classes as shown below �is there any problem of concurrency?? Means multiple session bean instances inserting data in the same table using the helper class will create any problems?? I am using MySql db presently. Or all will work fine coz I am using the data source and pool available in welogic app server?
Is this a good approach if my application is doing inserts 90% of times? or I have to use entity beans or serialized objects encapsulating each class?
public class Logger implements SessionBean
DAO dao = getDAO();
dao.insertXyzLog(�x�, �y�,�.);
private DAO getDAO(){
if(Dao == null) {
oao = DAOFactory.getDAO();
return Dao;
//other std methods
public interface DAO {
// methods to directly insert data in to the tables
//some methods to look for required value in another tables
public abstract void insertXyzLog (String x, Stringy, ���.);
public class DAOImpl implements DAO {
// look up for JNDI data sourse
//method to return connection
public void insertXyzLog (String x, Stirng y�){
//SQLs for inserting into Xyz table using connection obtained above.Hi,
Nothing wrong in using Helper class to insert into table. It won't create problem as long as your database server able to handle that many request from client.
If you use weblogic server and datasource, the server will take care of all connection pool management depending upon your configuration parameters.
Moreover, insert won't lock the table. So you need not worry about those things.
Best Luck,
Senthil Babu
Developer Technical Support
SUN Microsystems
http://www.sun.com/developers/support/ -
Serialize tree in random access file
I have a tree of objects, which are to be serialized. After deserialization,
each object has to know its children and its parent. Each object need not know its sibling. There are no lateral references. The number of objects in the tree will be around 2000. The depth of tree is 7. Different parts of the tree will be saved and read at different points of time in the application. While the application is running, many of the objects in the tree keep undergoing changes which have to be saved intermittantly.
I would like to use a random access file. But I have no clue how to serialize a tree of objects in a random access file which will meet the above mentioned requirements.
I would be thankful if I could get an idea to implement.I have a tree of objects, which are to be serialized.
After deserialization,
each object has to know its children and its parent.
Each object need not know its sibling. There are no
lateral references. The number of objects in the tree
will be around 2000. The depth of tree is 7.
Different parts of the tree will be saved and read at
different points of time in the application. This is an issue. When you Serialize something you serialize the entire object, not a part of it. If you're doing this then you're no longer talking about Serializing a Tree, you're talking about serializing some other thing that represents part of the tree.
While
the application is running, many of the objects in
the tree keep undergoing changes which have to be
saved intermittantly.
I would like to use a random access file. But I have
no clue how to serialize a tree of objects in a
random access file which will meet the above
mentioned requirements.
I would be thankful if I could get an idea to
implement.If you can come up with a "savable" representation of your tree that allows you to calculate where, in a file, a given part would be then you could use RandomAccessFile to read/write the thing. Just walk through your tree and seek to where you need to go in the file, then read/write the node.
I hope that made sense
Lee -
This program is supposed to retrieve data from TelephoneUI's JTextFields, store the data in a RandomAccessRecord class object record and call the write method of class RandomAccessRecord to output the data.
Two questions
Is there any way to check if the data was actually written to the file without writing a new class to read the file?
How can I make it so that the file-position pointer for object output start from byte 0 for the first record and move 94 bytes after each read? Right now the file-position pointer moves 94 bytes after each read but starts from 94.
public class WriteRandomFile extends JFrame {
private RandomAccessFile output;
private TelephoneUI userInterface;
private JButton enterButton, openButton;
// set up GUI
public WriteRandomFile()
super( "Write to random access file" );
// create instance of reusable user interface TelephoneUI
userInterface = new TelephoneUI(4); // four textfields
getContentPane().add( userInterface,
BorderLayout.CENTER );
// get reference to generic task button doTask1 in TelephoneUI
openButton = userInterface.getDoTask1Button();
openButton.setText( "Open..." );
// register listener to call openFile when button pressed
openButton.addActionListener(
// anonymous inner class to handle openButton event
new ActionListener() {
// allow user to select file to open
public void actionPerformed( ActionEvent event )
openFile();
} // end anonymous inner class
); // end call to addActionListener
// register window listener for window closing event
addWindowListener(
// anonymous inner class to handle windowClosing event
new WindowAdapter() {
// add record in TelephoneUI, then close file
public void windowClosing( WindowEvent event )
if ( output != null )
addRecord();
closeFile();
} // end anonymous inner class
); // end call to addWindowListener
// get reference to generic task button doTask2 in TelephoneUI
enterButton = userInterface.getDoTask2Button();
enterButton.setText( "Enter" );
enterButton.setEnabled( false );
// register listener to call addRecord when button pressed
enterButton.addActionListener(
// anonymous inner class to handle enterButton event
new ActionListener() {
// add record to file
public void actionPerformed( ActionEvent event )
addRecord();
} // end anonymous inner class
); // end call to addActionListener
setSize( 300, 150 );
show();
// enable user to choose file to open
private void openFile()
// display file dialog so user can select file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// if user clicked Cancel button on dialog, return
if ( result == JFileChooser.CANCEL_OPTION )
return;
// obtain selected file
File fileName = fileChooser.getSelectedFile();
// display error if file name invalid
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// open file
try {
output = new RandomAccessFile( fileName, "rw" );
enterButton.setEnabled( true );
openButton.setEnabled( false );
// process exception while opening file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"File does not exist",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE );
} // end method openFile
// close file and terminate application
private void closeFile()
// close file and exit
try {
if ( output != null )
output.close();
System.exit( 0 );
// process exception while closing file
catch( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error closing file",
"Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
// add one record to file
public void addRecord()
String fields[] = userInterface.getFieldValues();
RandomAccessRecord record =
new RandomAccessRecord();
int count = 1;
// ensure field has a value
if ( ! fields[ TelephoneUI.LASTNAME ].equals( "" ) ) {
// output values to file
try {
record.setFirstName( fields[ TelephoneUI.FIRSTNAME ] );
record.setLastName( fields[ TelephoneUI.LASTNAME ] );
record.setAddress( fields[ TelephoneUI.ADDRESS ] );
record.setPhone( Integer.parseInt( fields[ TelephoneUI.PHONENUMBER ] ) );
count = count * 94;
output.seek( count );
record.write( output );
userInterface.clearFields(); // clear TextFields
// process improper account number or balance format
catch ( NumberFormatException formatException ) {
JOptionPane.showMessageDialog( this,
"Bad account number or balance",
"Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
// process exceptions while writing to file
catch ( IOException ioException ) {
closeFile();
} // end method addRecord
// execute application
public static void main( String args[] )
new WriteRandomFile();
} // end class WriteRandomFileThis program is supposed to retrieve data from
TelephoneUI's JTextFields, store the data in a
RandomAccessRecord class object record and call the
write method of class RandomAccessRecord to output
the data.
Two questions
Is there any way to check if the data was actually
written to the file without writing a new class to
read the file? Why? If the io operations are not working (and not throwing exceptions) then you might as well give up.
>
How can I make it so that the file-position pointer
for object output start from byte 0 for the first
record and move 94 bytes after each read? Right now
the file-position pointer moves 94 bytes after each
read but starts from 94.Huh? It moves 94 and you want it to move 94?
Perhaps you are just looking for the getFilePointer() and seek() methods?
>
public class WriteRandomFile extends JFrame {
private RandomAccessFile output;
private TelephoneUI userInterface;
private JButton enterButton, openButton;
// set up GUI
public WriteRandomFile()
super( "Write to random access file" );
// create instance of reusable user interface
erface TelephoneUI
userInterface = new TelephoneUI(4); // four
/ four textfields
getContentPane().add( userInterface,
BorderLayout.CENTER );
// get reference to generic task button doTask1
oTask1 in TelephoneUI
openButton = userInterface.getDoTask1Button();
openButton.setText( "Open..." );
// register listener to call openFile when
e when button pressed
openButton.addActionListener(
// anonymous inner class to handle
to handle openButton event
new ActionListener() {
// allow user to select file to open
public void actionPerformed( ActionEvent
ActionEvent event )
openFile();
} // end anonymous inner class
); // end call to addActionListener
// register window listener for window closing
losing event
addWindowListener(
// anonymous inner class to handle
to handle windowClosing event
new WindowAdapter() {
// add record in TelephoneUI, then close
, then close file
public void windowClosing( WindowEvent
WindowEvent event )
if ( output != null )
addRecord();
closeFile();
} // end anonymous inner class
); // end call to addWindowListener
// get reference to generic task button doTask2
oTask2 in TelephoneUI
enterButton =
tton = userInterface.getDoTask2Button();
enterButton.setText( "Enter" );
enterButton.setEnabled( false );
// register listener to call addRecord when
d when button pressed
enterButton.addActionListener(
// anonymous inner class to handle
to handle enterButton event
new ActionListener() {
// add record to file
public void actionPerformed( ActionEvent
ActionEvent event )
addRecord();
} // end anonymous inner class
); // end call to addActionListener
setSize( 300, 150 );
show();
// enable user to choose file to open
private void openFile()
// display file dialog so user can select file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this
( this );
// if user clicked Cancel button on dialog,
ialog, return
if ( result == JFileChooser.CANCEL_OPTION )
return;
// obtain selected file
File fileName = fileChooser.getSelectedFile();
// display error if file name invalid
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name", "Invalid File
Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// open file
try {
output = new RandomAccessFile( fileName,
e( fileName, "rw" );
enterButton.setEnabled( true );
openButton.setEnabled( false );
// process exception while opening file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"File does not exist",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE );
} // end method openFile
// close file and terminate application
private void closeFile()
// close file and exit
try {
if ( output != null )
output.close();
System.exit( 0 );
// process exception while closing file
catch( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error closing file",
"Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
// add one record to file
public void addRecord()
String fields[] =
ds[] = userInterface.getFieldValues();
RandomAccessRecord record =
new RandomAccessRecord();
int count = 1;
// ensure field has a value
if ( ! fields[ TelephoneUI.LASTNAME ].equals(
quals( "" ) ) {
// output values to file
try {
record.setFirstName( fields[
stName( fields[ TelephoneUI.FIRSTNAME ] );
record.setLastName( fields[
stName( fields[ TelephoneUI.LASTNAME ] );
record.setAddress( fields[
ddress( fields[ TelephoneUI.ADDRESS ] );
record.setPhone( Integer.parseInt(
teger.parseInt( fields[ TelephoneUI.PHONENUMBER ] )
count = count * 94;
output.seek( count );
record.write( output );
userInterface.clearFields(); // clear
); // clear TextFields
// process improper account number or
number or balance format
catch ( NumberFormatException
Exception formatException ) {
JOptionPane.showMessageDialog( this,
"Bad account number or balance",
"Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
// process exceptions while writing to file
catch ( IOException ioException ) {
closeFile();
} // end method addRecord
// execute application
public static void main( String args[] )
new WriteRandomFile();
} // end class WriteRandomFile -
How to access last object in a Collection.
Hello. I'm trying to figure out how to access the last element in a collection. This is how I have it coded now:
Collection coll = someMethodThatReturnsACollectionOfMyTypes( );
Iterator It = coll.iterator( );
MyType myType = null;
while ( It.hasNext( ) )
myType = ( MyType) It.next( );
At this point myType should reference the object I'm interested in, but there has to be an easier way. Any suggestions would be greatly appreciated.
-JasonContainers either are or aren't expected to support random access, depending on the container. For those containers that do support it, like ArrayList, you can do the following:
public Object getLast(AbstractList a) {
if (0==a.size()) return null;
return a.get(a.size()-1);
} -
Serialized Objects and Servlets
I'm having a lot of trouble accessing a serialized object and displaying it in a servlet. I get the following exception:
java.io.StreamCorruptedException: Type code out of range, is -84
Can anyone help me out here?OK... here's some of the code I'm using...
public Object readObject()
throws IOException, ClassNotFoundException
oin = new ObjectInputStream(fin);
Object obj = oin.readObject();
oin.close();
return obj;
public void writeObject(Object data)
throws IOException
oout = new ObjectOutputStream(fout);
oout.writeObject(data);
oout.close();
Essentially, my servlet creates and instance of an ObjectManager class. This ObjectManager then tries to execute the readObject method above.
The whole process comes to a grinding halt at this point:
Object obj = oin.readObject();
I think this problem has to do with the way that I'm trying to access the file to which I have serialized my object. The problem is that I can't think of any other way to do it. Here's how I am currently referencing the file that holds my serialized object:
File theFile = new File("Serialized.dat");
FileInputStream fin = new FileInputStream(theFile);
I've never attempted anything like this before and I suspect I'm way off in my approach. I really appreciate your willingness to help me out. -
Using serialized objects for 'static' perso
Hi,
I guess this is food for thought. I am looking at the following avenue.
use serialized objects - compliant with the back office software - as 'static' perso. by static I mean all perso data that the card simply stores and does not need to use/modify to function.
These objects could change over time (data and methods) without having to re-issue card applications; and many "genertions" of cards could exist on the field as the back office software would know how to adjut to them.
Has this been done/looked at before ?
Regards,
PhilippeHi,
What I meant was use objects serialized by software (outside of the card) to personalize easily all data that:
1) have the same access security level
2) do not need any modification/understanding from the applet
My post is quite old now ...
I actually have done it and it is quite useful, at least to setup prototypes.
Regards,
Philippe -
Why increase db env cache not improve random access performance?
I want to improve bdb random access performance.
I built a bdb with 3,000,000 records and then test random access on these records. I assign db env cache with 100M, 200M and 500M. With more cache, the cache hit ratio has been increased. However, the overall performance is NOT improved (actually performance is a little poorer with bigger cache). Why does this happen? Any way to improve the performance?
I plan to change page size later. Any suggestion on the page size setting?Below is the db_stat output of 100M cache.
Thu Feb 25 07:02:37 2010 Local time
53162 Btree magic number
9 Btree version number
Little-endian Byte order
multiple-databases Flags
2 Minimum keys per-page
4096 Underlying database page size
1007 Overflow key/data size
4 Number of levels in the tree
3000000 Number of unique keys in the tree
3000000 Number of data items in the tree
546 Number of tree internal pages
42286 Number of bytes free in tree internal pages (98% ff)
48388 Number of tree leaf pages
4939160 Number of bytes free in tree leaf pages (97% ff)
0 Number of tree duplicate pages
0 Number of bytes free in tree duplicate pages (0% ff)
3000000 Number of tree overflow pages
2259M Number of bytes free in tree overflow pages (81% ff)
0 Number of empty pages
0 Number of pages on the free list
125MB 2KB 24B Total cache size
1 Number of caches
1 Maximum number of caches
125MB 8KB Pool individual cache size
0 Maximum memory-mapped file size
0 Maximum open file descriptors
0 Maximum sequential buffer writes
0 Sleep after writing maximum sequential buffers
0 Requested pages mapped into the process' address space
9924074 Requested pages found in the cache (54%)
8125037 Requested pages not found in the cache
0 Pages created in the cache
8125037 Pages read into the cache
0 Pages written from the cache to the backing file
8094176 Clean pages forced from the cache
0 Dirty pages forced from the cache
0 Dirty pages written by trickle-sync thread
30861 Current total page count
30860 Current clean page count
1 Current dirty page count
16381 Number of hash buckets used for page location
26M Total number of times hash chains searched for a page (26174148)
132 The longest hash chain searched for a page
53M Total number of hash chain entries checked for page (53243413)
0 The number of hash bucket locks that required waiting (0%)
0 The maximum number of times any hash bucket lock was waited for (0%)
0 The number of region locks that required waiting (0%)
0 The number of buffers frozen
0 The number of buffers thawed
0 The number of frozen buffers freed
8125049 The number of page allocations
17M The number of hash buckets examined during allocations (17891415)
12 The maximum number of hash buckets examined for an allocation
8094176 The number of pages examined during allocations
1 The max number of pages examined for an allocation
0 Threads waited on page I/O
Pool File: md.bdbxml
16384 Page size
0 Requested pages mapped into the process' address space
131 Requested pages found in the cache (90%)
14 Requested pages not found in the cache
0 Pages created in the cache
14 Pages read into the cache
0 Pages written from the cache to the backing file
Pool File: md.bdbdb
4096 Page size
0 Requested pages mapped into the process' address space
9923933 Requested pages found in the cache (54%)
8125020 Requested pages not found in the cache
0 Pages created in the cache
8125020 Pages read into the cache
0 Pages written from the cache to the backing file
Pool File: compMd.bdbdb
4096 Page size
0 Requested pages mapped into the process' address space
10 Requested pages found in the cache (76%)
3 Requested pages not found in the cache
0 Pages created in the cache
3 Pages read into the cache
0 Pages written from the cache to the backing file
Default locking region information:
158 Last allocated locker ID
0x7fffffff Current maximum unused locker ID
9 Number of lock modes
1000 Maximum number of locks possible
1000 Maximum number of lockers possible
1000 Maximum number of lock objects possible
40 Number of lock object partitions
14 Number of current locks
104 Maximum number of locks at any one time
4 Maximum number of locks in any one bucket
0 Maximum number of locks stolen by for an empty partition
0 Maximum number of locks stolen for any one partition
20 Number of current lockers
35 Maximum number of lockers at any one time
13 Number of current lock objects
101 Maximum number of lock objects at any one time
3 Maximum number of lock objects in any one bucket
0 Maximum number of objects stolen by for an empty partition
0 Maximum number of objects stolen for any one partition
12M Total number of locks requested (12049088)
12M Total number of locks released (12049068)
0 Total number of locks upgraded
35 Total number of locks downgraded
0 Lock requests not available due to conflicts, for which we waited
0 Lock requests not available due to conflicts, for which we did not wait
0 Number of deadlocks
0 Lock timeout value
0 Number of locks that have timed out
0 Transaction timeout value
0 Number of transactions that have timed out
736KB The size of the lock region
0 The number of partition locks that required waiting (0%)
0 The maximum number of times any partition lock was waited for (0%)
0 The number of object queue operations that required waiting (0%)
0 The number of locker allocations that required waiting (0%)
0 The number of region locks that required waiting (0%)
3 Maximum hash bucket length
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Lock REGINFO information:
Lock Region type
5 Region ID
__db.005 Region name
0x7fc142d1c000 Original region address
0x7fc142d1c000 Region address
0x7fc142d1c138 Region primary address
0 Region maximum allocation
0 Region allocated
Region allocations: 3006 allocations, 0 failures, 0 frees, 1 longest
Allocations by power-of-two sizes:
1KB 3002
2KB 0
4KB 1
8KB 0
16KB 0
32KB 2
64KB 1
128KB 0
256KB 0
512KB 0
1024KB 0
REGION_JOIN_OK Region flags
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Lock region parameters:
32792 Lock region region mutex [0/28 0% 18243/140468027311840]
1031 locker table size
1031 object table size
824 obj_off
73640 locker_off
0 need_dd
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Lock conflict matrix:
0 0 0 0 0 0 0 0 0
0 0 1 0 1 0 1 0 1
0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 1 1
0 0 1 0 0 0 0 0 1
0 1 1 0 0 0 0 1 1
0 0 1 0 1 0 1 0 0
0 1 1 0 1 1 1 0 1
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Locks grouped by lockers:
Locker Mode Count Status ----------------- Object ---------------
42 dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
42 READ 1 HELD md.bdbxml:secondary_ handle 2
45 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
46 dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
46 READ 1 HELD md.bdbxml:secondary_ handle 4
49 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
4a dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
4a READ 1 HELD md.bdbxml:secondary_ handle 6
4d dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
4e dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
4e READ 1 HELD md.bdbxml:secondary_ handle 8
51 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
52 dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
52 READ 1 HELD md.bdbxml:secondary_ handle 10
55 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
56 dd= 0 locks held 2 write locks 0 pid/thread 19176/139654678157040
56 READ 1 HELD md.bdbxml:secondary_ handle 12
56 READ 6 HELD md.bdbxml:secondary_ handle 0
59 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
5d dd= 0 locks held 1 write locks 0 pid/thread 19176/139654678157040
5d READ 1 HELD md.bdbxml:secondary_ handle 14
60 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
61 dd= 0 locks held 2 write locks 0 pid/thread 19176/139654678157040
61 READ 1 HELD md.bdbxml:secondary_ handle 16
61 READ 2 HELD md.bdbxml:secondary_ handle 0
64 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
95 dd= 0 locks held 2 write locks 0 pid/thread 19176/139654678157040
95 READ 1 HELD md.bdbdb:md.db handle 2
95 READ 1 HELD md.bdbdb:md.db handle 0
98 dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
99 dd= 0 locks held 2 write locks 0 pid/thread 19176/139654678157040
99 READ 1 HELD compMd.bdbdb:compMd.bdb handle 2
99 READ 1 HELD compMd.bdbdb:compMd.bdb handle 0
9c dd= 0 locks held 0 write locks 0 pid/thread 19176/139654678157040
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Locks grouped by object:
Locker Mode Count Status ----------------- Object ---------------
61 READ 1 HELD md.bdbxml:secondary_ handle 16
4a READ 1 HELD md.bdbxml:secondary_ handle 6
46 READ 1 HELD md.bdbxml:secondary_ handle 4
42 READ 1 HELD md.bdbxml:secondary_ handle 2
56 READ 6 HELD md.bdbxml:secondary_ handle 0
61 READ 2 HELD md.bdbxml:secondary_ handle 0
5d READ 1 HELD md.bdbxml:secondary_ handle 14
56 READ 1 HELD md.bdbxml:secondary_ handle 12
52 READ 1 HELD md.bdbxml:secondary_ handle 10
4e READ 1 HELD md.bdbxml:secondary_ handle 8
95 READ 1 HELD md.bdbdb:md.db handle 0
95 READ 1 HELD md.bdbdb:md.db handle 2
99 READ 1 HELD compMd.bdbdb:compMd.bdb handle 0
99 READ 1 HELD compMd.bdbdb:compMd.bdb handle 2 -
Random Access File not working, Need Help!!!!
I am having trouble creating and displaying a Random Access File for hardware tools. I have included the code below in eight files:
// Exercise 14.11: HardwareRecord.java
package org.egan; // packaged for reuse
public class HardwareRecord
private int recordNumber;
private String toolName;
private int quantity;
private double cost;
// no-argument constructor calls other constructor with default values
public HardwareRecord()
this(0,"",0,0.0); // call four-argument constructor
} // end no-argument HardwareRecord constructor
// initialize a record
public HardwareRecord(int number, String tool, int amount, double price)
setRecordNumber(number);
setToolName(tool);
setQuantity(amount);
setCost(price);
} // end four-argument HardwareRecord constructor
// set record number
public void setRecordNumber(int number)
recordNumber = number;
} // end method setRecordNumber
// get record number
public int getRecordNumber()
return recordNumber;
} // end method getRecordNumber
// set tool name
public void setToolName(String tool)
toolName = tool;
} // end method setToolName
// get tool name
public String getToolName()
return toolName;
} // end method getToolName
// set quantity
public void setQuantity(int amount)
quantity = amount;
} // end method setQuantity
// get quantity
public int getQuantity()
return quantity;
} // end method getQuantity
// set cost
public void setCost(double price)
cost = price;
} // end method setCost
// get cost
public double getCost()
return cost;
} // end method getCost
} // end class HardwareRecord------------------------------------------------------------------------------------------------
// Exercise 14.11: RandomAccessHardwareRecord.java
// Subclass of HardwareRecord for random-access file programs.
package org.egan; // package for reuse
import java.io.RandomAccessFile;
import java.io.IOException;
public class RandomAccessHardwareRecord extends HardwareRecord
public static final int SIZE = 72;
// no-argument constructor calls other constructor with default values
public RandomAccessHardwareRecord()
this(0,"",0,0.0);
} // end no-argument RandomAccessHardwareRecord constructor
// initialize a RandomAccessHardwareRecord
public RandomAccessHardwareRecord(int number, String tool, int amount, double price)
super(number,tool,amount,price);
} // end four-argument RandomAccessHardwareRecord constructor
// read a record from a specified RandomAccessFile
public void read(RandomAccessFile file) throws IOException
setRecordNumber(file.readInt());
setToolName(readName(file));
setQuantity(file.readInt());
setCost(file.readDouble());
} // end method read
// ensure that name is proper length
private String readName(RandomAccessFile file) throws IOException
char name[] = new char[15], temp;
for(int count = 0; count < name.length; count++)
temp = file.readChar();
name[count] = temp;
} // end for
return new String(name).replace('\0',' ');
} // end method readName
// write a record to specified RandomAccessFile
public void write(RandomAccessFile file) throws IOException
file.writeInt(getRecordNumber());
writeName(file, getToolName());
file.writeInt(getQuantity());
file.writeDouble(getCost());
} // end method write
// write a name to file; maximum of 15 characters
private void writeName(RandomAccessFile file, String name) throws IOException
StringBuffer buffer = null;
if (name != null)
buffer = new StringBuffer(name);
else
buffer = new StringBuffer(15);
buffer.setLength(15);
file.writeChars(buffer.toString());
} // end method writeName
} // end RandomAccessHardwareRecord------------------------------------------------------------------------------------------------
// Exercise 14.11: CreateRandomFile.java
// creates random-access file by writing 100 empty records to disk.
import java.io.IOException;
import java.io.RandomAccessFile;
import org.egan.RandomAccessHardwareRecord;
public class CreateRandomFile
private static final int NUMBER_RECORDS = 100;
// enable user to select file to open
public void createFile()
RandomAccessFile file = null;
try // open file for reading and writing
file = new RandomAccessFile("hardware.dat","rw");
RandomAccessHardwareRecord blankRecord = new RandomAccessHardwareRecord();
// write 100 blank records
for (int count = 0; count < NUMBER_RECORDS; count++)
blankRecord.write(file);
// display message that file was created
System.out.println("Created file hardware.dat.");
System.exit(0); // terminate program
} // end try
catch (IOException ioException)
System.err.println("Error processing file.");
System.exit(1);
} // end catch
finally
try
if (file != null)
file.close(); // close file
} // end try
catch (IOException ioException)
System.err.println("Error closing file.");
System.exit(1);
} // end catch
} // end finally
} // end method createFile
} // end class CreateRandomFile-------------------------------------------------------------------------------------------------
// Exercise 14.11: CreateRandomFileTest.java
// Testing class CreateRandomFile
public class CreateRandomFileTest
// main method begins program execution
public static void main( String args[] )
CreateRandomFile application = new CreateRandomFile();
application.createFile();
} // end main
} // end class CreateRandomFileTest-------------------------------------------------------------------------------------------------
// Exercise 14.11: WriteRandomFile.java
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.NoSuchElementException;
import java.util.Scanner;
import org.egan.RandomAccessHardwareRecord;
public class WriteRandomFile
private RandomAccessFile output;
private static final int NUMBER_RECORDS = 100;
// enable user to choose file to open
public void openFile()
try // open file
output = new RandomAccessFile("hardware.dat","rw");
} // end try
catch (IOException ioException)
System.err.println("File does not exist.");
} // end catch
} // end method openFile
// close file and terminate application
public void closeFile()
try // close file and exit
if (output != null)
output.close();
} // end try
catch (IOException ioException)
System.err.println("Error closing file.");
System.exit(1);
} // end catch
} // end method closeFile
// add records to file
public void addRecords()
// object to be written to file
RandomAccessHardwareRecord record = new RandomAccessHardwareRecord();
int recordNumber = 0;
String toolName;
int quantity;
double cost;
Scanner input = new Scanner(System.in);
System.out.printf("%s\n%s\n%s\n%s\n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter");
System.out.printf("%s %s\n%s", "Enter record number (1-100),",
"tool name, quantity and cost.","? ");
while (input.hasNext())
try // output values to file
recordNumber = input.nextInt(); // read record number
toolName = input.next(); // read tool name
quantity = input.nextInt(); // read quantity
cost = input.nextDouble(); // read cost
if (recordNumber > 0 && recordNumber <= NUMBER_RECORDS)
record.setRecordNumber(recordNumber);
record.setToolName(toolName);
record.setQuantity(quantity);
record.setCost(cost);
output.seek((recordNumber - 1) * // position to proper
RandomAccessHardwareRecord.SIZE); // location for file
record.write(output);
} // end if
else
System.out.println("Account must be between 0 and 100.");
} // end try
catch (IOException ioException)
System.err.println("Error writing to file.");
return;
} // end catch
catch (NoSuchElementException elementException)
System.err.println("Invalid input. Please try again.");
input.nextLine(); // discard input so enter can try again
} // end catch
System.out.printf("%s %s\n%s","Enter record number (1-100),",
"tool name, quantity and cost.", "? ");
} // end while
} // end method addRecords
} // end class WriteRandomFile-------------------------------------------------------------------------------------------------
// Exercise 14.11: WriteRandomFileTest.java
// Testing class WriteRandomFile
public class WriteRandomFileTest
// main method begins program execution
public static void main( String args[] )
WriteRandomFile application = new WriteRandomFile();
application.openFile();
application.addRecords();
application.closeFile();
} // end main
} // end class WriteRandomFileTest-------------------------------------------------------------------------------------------------
// Exercise 14.11: ReadRandomFile.java
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.egan.RandomAccessHardwareRecord;
public class ReadRandomFile
private RandomAccessFile input;
// enable user to select file to open
public void openFile()
try // open file
input = new RandomAccessFile("hardware.dat","r");
} // end try
catch (IOException ioException)
System.err.println("File does not exist.");
} // end catch
} // end method openFile
// read and display records
public void readRecords()
RandomAccessHardwareRecord record = new RandomAccessHardwareRecord();
System.out.printf("%-10s%-15s%-15s%10s\n","Record","Tool Name","Quantity","Cost");
try // read a record and display
while(true)
do
record.read(input);
}while (record.getRecordNumber() == 0);
// display record contents
System.out.printf("%-10d%-12s%-12d%10.2f\n", record.getRecordNumber(),
record.getToolName(), record.getQuantity(), record.getCost());
} // end while
} // end try
catch (EOFException eofException)
return; // end of file was reached
} // end catch
catch (IOException ioException)
System.err.println("Error reading file.");
System.exit(1);
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
try // close file and exit
if (input != null)
input.close();
} // end try
catch (IOException ioException)
System.err.println("Error closing file.");
System.exit(1);
} // end catch
} // end methode closeFile
} // end class ReadRandomFile-------------------------------------------------------------------------------------------------
// Exercise 14.11: ReadRandomFileTest.java
// Testing class ReadRandomFile
public class ReadRandomFileTest
// main method begins program execution
public static void main( String args[] )
ReadRandomFile application = new ReadRandomFile();
application.openFile();
application.readRecords();
application.closeFile();
} // end main
} // end class ReadRandomFileTest-------------------------------------------------------------------------------------------------
Below is the sample data to be inputted in the random file:
Record Tool Name Quantity Cost
Number
3 Sander 18 35.99
19 Hammer 128 10.00
26 Jigsaw 16 14.25
39 Mower 10 79.50
56 Saw 8 89.99
76 Screwdriver 236 4.99
81 Sledgehammer 32 19.75
88 Wrench 65 6.48I have managed to fix your program.
The solution
The records are sized by the various Writes that occur.
A record is an int + 15 chars + int + double.
WriteInt writes 4 bytes
WriteChar (Called by WriteChars) write 2 bytes
WriteDouble writes 8 bytes.
(In Java 1.5 )
4 bytes + 30 Bytes + 4Bytes + 8 Bytes. = 46 Bytes.
The details are in the API for Random Acces Files at
http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html
The code for RandomAccessHardwareRecord line
public statis final int SIZE = 72needs to have the 72 changed to 46
This should make your code work.
I have hacked around with some other bits and will send you my code if you want but that is the key. The asnwers you were getting illustrated a bunch of bytes being read as (say) an int and beacuse of the wrong record length, they were just a bunch of 4 bytes that evaluated to whetever was at that point in the file.
When the record was written the line
output.seek((recordNumber -1 ) * RandomAccessHardwareRecord.SIZE);had SIZE as 72 and so the seek operation stuck the file pointer in the wrong place.
This kind of stuff is good fun and good learning for mentally getting a feel for record filing but in real problems you either serialize your objects or use XML (better) or use jdbc (possibley even better depending on what you are doing).
I would echo sabre comment about the program being poor though because
If the program is meant to teach, it is littered with overly complex statements and if it is meant to be a meaningful program, the objects are too tied to hardware and DAO is the way to go. The problem that the program has indicates that maybe it is maybe fairly old and not written with java 2 in mind.
As for toString() and "Yuk"
Every class inherits toString() from Object. so if you System.out.println(Any object) then you will get something printed. What gets printed is determined by a complex hieracrchy of classes unless you overRide it with your own method.
If you use UnitTesting (which would prevent incorrect code getting as far as this code did in having an error), then toString() methods are really very useful.
Furthermore, IMO Since RandomAccessHardwareRecord knows how to file itself then I hardly think that knowing how to print itself to the console is a capital offence.
In order to expand on the 72 / 46 byte problem.
Message was edited by:
nerak99 -
After around 30min of having FF open when I close a tab a message pops up:
Javascript application
Unable to clean current element: TypeError: can't access dead object
OK
It randomly stops showing the message for periods of about 30 min.hello MichaelStevens, this is very likely caused by one of your addons (maybe one which is performing a certain action every 30mins).
[[Troubleshoot extensions, themes and hardware acceleration issues to solve common Firefox problems]]
https://blog.mozilla.org/addons/2012/09/12/what-does-cant-access-dead-object-mean/ -
Random results with random access query
Hi,
when I execute a random access query
(fetchConfig.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
fetchConfig.setFetchDirection(ResultSet.FETCH_UNKNOWN)) and iterate over the
result set twice, the second time the objects are retrieved in a different
order, also when I set the ordering of the objects. This only happens if the
first loop touches the last item. If not, everything works fine. I am using
MS Sql Server, microsoft driver, kodo 3.4
regards,
ChristiaanHi,
I am using datastore identity. I do implement equals and hashcode in the
super class, not sure whether that is of any impact. The order of the items
look random to me, however it stays constant after the second loop. The same
order is also produced when I re-start the test application. The output
shows the index of the object I am retrieving, the description of the object
(which is also the order in which the items are created) and their order
attribute. The query sorts on the order attribute. As you can see the first
loop is correct. I now and then have the situation where the last item in
the first loop (retrieve 7) results in the first item (item nr 0) being
retrieved. Same behaviour applies for both jtds and microsoft driver.
first loop:
retrieve 0: item nr: 0 order: 0
retrieve 1: item nr: 1 order: 1
retrieve 2: item nr: 2 order: 2
retrieve 3: item nr: 3 order: 3
retrieve 4: item nr: 4 order: 4
retrieve 5: item nr: 5 order: 5
retrieve 6: item nr: 6 order: 6
retrieve 7: item nr: 7 order: 7
second loop
retrieve 0: item nr: 3 order: 3
retrieve 1: item nr: 6 order: 6
retrieve 2: item nr: 7 order: 7
retrieve 3: item nr: 0 order: 0
retrieve 4: item nr: 5 order: 5
retrieve 5: item nr: 4 order: 4
retrieve 6: item nr: 2 order: 2
retrieve 7: item nr: 1 order: 1
third loop
retrieve 0: item nr: 3 order: 3
retrieve 1: item nr: 6 order: 6
retrieve 2: item nr: 7 order: 7
retrieve 3: item nr: 0 order: 0
retrieve 4: item nr: 5 order: 5
retrieve 5: item nr: 4 order: 4
retrieve 6: item nr: 2 order: 2
retrieve 7: item nr: 1 order: 1
"Marc Prud'hommeaux" <[email protected]> wrote in message
news:[email protected]...
Christiaan-
My first guess is are you using application identity, and if so, are you
correctly implementing the equals() and hashCode() method? Failure to do
so may conceivably cause this problem.
Otherwise, can you provide some details about how the results are our of
order? It it exactly reverse or something, or are elements in random
order?
Christiaan wrote:
Hi,
when I execute a random access query
(fetchConfig.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
fetchConfig.setFetchDirection(ResultSet.FETCH_UNKNOWN)) and iterate over
the result set twice, the second time the objects are retrieved in a
different order, also when I set the ordering of the objects. This only
happens if the first loop touches the last item. If not, everything works
fine. I am using MS Sql Server, microsoft driver, kodo 3.4
regards,
Christiaan
Marc Prud'hommeaux
SolarMetric Inc. -
Random access of ObjectOutputStream
hi in my application i am writing array of Sets to a File through ObjectOutputStream
is there any way i can randomly access the file and get(say) 3rd Object written to that File and Read that Object
is there any way please help
very urgent for my project>
thanks for your reply >You can best express your gratitude & thanks by putting an upper case letter at the start of each sentence(a). Also, one full stop '.' at the end of each sentence. These things make it easier for the reader(s), and you would not want to make it harder for people trying to help you, would you?
>
i finished the project >( (a) And also upper case for the word 'I', wherever it appears in a sentence. )
What, you solved that stumbling block of reading the Objects in random order, in the (checks watch) 20 minutes since you posted the question? Congratulations!
>
but the problem is since i have to read whole array it takes more time to read the object and it uses more memory
is there any other way to do it?>Oh, so you mean 'finished except for the show stopping problem'. You have a humorous concept of 'finished'. ;-)
Two possible alternatives:
1) Use an ObjectInputStream and check the performance when reading the entire stream sequentially to get to the Object of interest.
2) Use a ByteArrayInput/OutputStream to do the entire thing within memory.
Of course, if we knew more about the program feature you were attempting to offer, and the problem domain, it might help us to guide you to good strategies for achieving the feature, within the constraints of the problem domain. -
any ideas about how to create a random access set? It'd be nice to have a class that implements a Set and a List.
How would you do something like this? I read somewhere in the API docs that you wouldn't be able to over-ride equals properly or something?The equals method for Set is the usual definition of Set-equality: a Set can only equal another Set, and then only when they have the same size and exactly the same members. A List can only equal another List, and then only when they have the same size and the same sequence of members. So an object can't have both types.
On the other hand, nothing stops a Set from having a random-access operation like get(). Just document that equal Sets may return differenet elements: after all, iterators provided by equal Sets don't have to provide the same sequence of members.
--Nax
Maybe you are looking for
-
Error message when loading home page
A month ago this problem developed. I have had My Yahoo.com as my Home page for years. Now when I open the Firefox Browser I get an error message that says: Invalid username/password combination. If you continue to get this error, try entering your e
-
ANN: Oracle XML Parser for Java v2.0.0.1
A new maintenance release of the Oracle Parser for Java is available for download. It has the following fixes and changes: Bug fixes for #920536, i.e. Cannot access element attributes via XSLT; #898423. i.e. ElementDecl's in DTDs; #774774, i.e. DOM e
-
Importing Database Package into OWB changes position of parameters .. Nasty
Hi all, Anyone else having the same problem ? Any solutions ? Or is this a bug. When a package is imported from a DB schema, OWB kindly changes the order in which the parameters are listed. The effect is that any mapping using the procedure or functi
-
AppleID not being recognized on newly set up iPad
Just finished setting up an iPad and tried downloading an app in the App Store. Received the prompt that "This AppleID has not been used in the iTunes store". Went to the iTunes store on the iPad and tried to login, but received the same prompt - "
-
How to call a procude in oracle and return an array
I need to call a procedure and return an array, can give some example code?