1

人为错误。下面的代码没有错误

主持人:请删掉。如果可能的话,请将我的 25 个名声发送给 Peter Lawrey,因为他帮助了我很多,甚至阅读了厚厚的日志数据。谢谢!

嗨,我在使用可序列化数据库时遇到问题。

关闭程序后(通过注销按钮),应该存储数据库。这是代码:

FileOutputStream f_out = null;
        try {
            f_out = new FileOutputStream("database.data");
            ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
            obj_out.writeObject(database);
            System.out.println("Stored!");

        } catch (Exception ex) {
            Logger.getLogger(BankMainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            try {
                f_out.close();
            } catch (IOException ex) {
                Logger.getLogger(BankMainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        new BankMainFrameGUI();
        this.dispose();

然后使用以下代码在登录时恢复数据库:

    try {
        FileInputStream f_in = new FileInputStream("database.data");
        ObjectInputStream obj_in = new ObjectInputStream(f_in);
        database = (BankSystem) obj_in.readObject();
        System.out.println("FOUND!");
    } catch (Exception e) {
        System.out.println("New database!");
        database = new BankSystem();
    }

数据库及其所有内部类已设置为可序列化。但是,重新启动程序后,所有数据都会丢失(客户、帐户、交易)。但是,数据库仍被检测为“找到!” 语句显示在我的编译器的 CLI 中,但不是“新数据库!” 陈述。

我使用了链表和数组列表。

非常感谢!我会升级所有可行的解决方案。

剧透:很多可能不必要的信息

如果需要,提供额外信息:

Main BankSystem() constructor + important variables:

public class BankSystem implements Serializable {
    Transaction transactionManager;
    Identity identityManager;
    Account accountManager;
    dayFirstTransaction dayFirstTransactionManager;


    public BankSystem() {
        transactionManager = new Transaction();
        identityManager = new Identity();
        accountManager = new Account();
        dayFirstTransactionManager = new dayFirstTransaction();
    }
...

LinkedList 节点示例:

class Transaction implements Serializable { //Database for TransactionNode
    TransactionNode lastTransaction = null;
    long lastTransactionId = -1;

    //Unchangable location settings defined. Prevents ability to manipulate timestamp by changing computer clock. Acts as a security feature.
    //Timestamps are also finalised to ensure security (prevent manipulation)
    final TimeZone tz = TimeZone.getTimeZone("Singapore");
    final Locale lc = new Locale("en", "SG");
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS EEEE");
    final private Calendar cal = Calendar.getInstance(tz, lc);

    public long newTransaction(dayFirstTransaction dFTM,
            Client identity, AccountNode account, boolean action, long actionAmount, 
            String location, String comments) throws IOException {
        //Setting timings
        long timeMilli = cal.getTimeInMillis();
        String timeStamp = dateFormat.format(cal.getTime());

        TransactionNode previous = lastTransaction;
        TransactionNode previousUserTransaction = account.GetLastTransaction();

        long transactionId = ++lastTransactionId;

        long initialBalance = account.getBalance();
        long finalBalance;
        if(action) {
            finalBalance = initialBalance + actionAmount;
        } else {
            if(initialBalance < actionAmount) throw new IOException("There is not enough funds present in account " + account.accountId + ". \nAvailable balance: $" + account.getBalance());
            finalBalance = initialBalance - actionAmount;
        }

        lastTransaction = new TransactionNode(previous, previousUserTransaction, timeMilli, timeStamp,  
            transactionId, identity, account, action, actionAmount, 
            initialBalance, finalBalance, location, comments);

        dFTM.checkTransaction(lastTransaction);
        account.setLastTransaction(lastTransaction);
        return transactionId;
    }

        public void newTransaction (dayFirstTransaction dFTM, long timeMilli, String timeStamp,  
            Client identity, AccountNode account, boolean action, long actionAmount, 
            long initialBalance, long finalBalance, String location, String comments) throws IOException {

        TransactionNode previous = lastTransaction;
        TransactionNode previousUserTransaction = account.GetLastTransaction();

        long transactionId = ++lastTransactionId;

        lastTransaction = new TransactionNode(previous, previousUserTransaction, timeMilli, timeStamp,  
            transactionId, identity, account, action, actionAmount, 
            initialBalance, finalBalance, location, comments);

        dFTM.checkTransaction(lastTransaction);
        account.setLastTransaction(lastTransaction);
    }
...

这个链表的节点:

class TransactionNode implements Serializable {
    //Node direction pointers
    final private TransactionNode previous;
    final private TransactionNode previousUserTransaction;

    //Declarations
    //For action:
    final public boolean CREDIT = true;
    final public boolean DEBIT = false;

    //Variables for each transaction
    final public long timeMilli; //Automated. For time comparisons
    final public String timeStamp;//Automated. For user reference
    final public long transactionId; // Automated at Transaction LinkedList
    final public Client identity;
    final public AccountNode account;
    final public boolean action; //true: Credit. false:Debit
    final public long actionAmount;
    final public long initialBalance;
    final public long finalBalance;
    final public String location;
    final public String comments;

    public TransactionNode(TransactionNode previous, TransactionNode previousUserTransaction, long timeMilli, String timeStamp,  
            long transactionId, Client identity, AccountNode account, boolean action, long actionAmount, 
            long initialBalance, long finalBalance, String location, String comments) {
        this.previous = previous;
        this.previousUserTransaction = previousUserTransaction;
        this.timeMilli = timeMilli;
        this.timeStamp = timeStamp;
        this.transactionId = transactionId;
        this.identity = identity;
        this.account = account;
        this.action = action;
        this.actionAmount = actionAmount;
        this.initialBalance = initialBalance;
        this.finalBalance = finalBalance;
        this.location = location;
        this.comments = comments;
    }

    //Getters & setters
    public TransactionNode getPreviousTransaction() {
        return previous;
    }

    public TransactionNode getPreviousUserTransaction() {
        return previousUserTransaction;
    }

}
4

4 回答 4

1

你确定你没有触发这个:

catch (Exception e) {
   System.out.println("New database!");
   database = new BankSystem();
}

至少会打印出与上述相关的异常和堆栈跟踪,即使它没有被触发。

我还将检查您写出的文件是否正在创建,并且是非零大小。这将为您提供一些关于数据是否正在被写入(当心 - 可能不完整)或未被读取的指标。

于 2012-08-21T09:52:16.470 回答
1

最可能的原因是您没有关闭写入的流。ObjectOutputStream 是缓冲的,如果您关闭它正在写入的文件而不是关闭它,您将不会写入所有数据,并且当您尝试读取它时会出现异常。

于 2012-08-21T09:54:14.873 回答
1
} catch (Exception e) {
    System.out.println("New database!");
    database = new BankSystem();
}

这是一个主要问题。这是处理反序列化异常的荒谬方式。而不是记录实际的异常和中止,你是:

  1. 完全忽略实际异常,这是有关实际问题的唯一信息来源。

  2. 就好像什么事都没发生过一样。

  3. 继续使用空数据库而不是空数据库,这让您的应用程序的其余部分继续运行,就好像除了一个神秘归零的数据库之外没有任何问题一样。

我什至根本不会捕捉到那个异常。我会让它通过构造函数甚至main()方法一直冒泡。

于 2012-08-21T10:06:54.403 回答
0
  1. 关闭流
  2. 确保每个类都实现serializable 了我们不可见的少数类ClientAccountNode
  3. 在每个异常中使用 printStackTrace() 方法。
于 2012-08-21T10:02:19.003 回答