[英]What happens with written data if Filechannel.force(true) throws an exception?
When filechannel.write was successful and we were able to write the whole buffer content into the filechannel i want to be sure if everything was flushed to disk. 当filechannel.write成功并且我们能够将整个缓冲区内容写入filechannel时,我想确定是否所有内容都已刷新到磁盘。 I'll call force(true), which can throw an exception.
我将调用force(true),它会引发异常。 What happens with data i wrote into filechannel?
我写入文件通道的数据会怎样? Was nothing flushed to disk or may be some part of data is flushed?
是否没有东西刷新到磁盘上,或者可能有一部分数据被刷新了? How can i rollback the operation?
如何回滚该操作?
public boolean flush(ByteBuffer[] buffers, long buffersRemaining)
{
try
{
FileChannel fileChannel = new RandomAccessFile(target, "rw").getChannel();
long writtenBytes = fileChannel.write(buffers);
//we think we have written all the data into filechannel
if(writtenBytes == buffersRemaining)
{
//we try to flush, but we get an exception
try
{
fileChannel.force(true);
fileChannel.close();
}
catch(IOException exception)
{
//???
return false;
}
}
}
catch(Exception exception)
{
//log exception
return false;
}
}
You should only try to handle exceptions if you can do something about them. 如果可以对异常进行某些处理,则应仅尝试处理它们。 This is one of these cases where it does not make sense.
这是其中没有意义的情况之一。 This should not happen normally, just log the exception and fail gracefully.
这通常不应该发生,只需记录异常并优雅地失败即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.