We get the exception :Cannot create a file when that file already exists
Hy all
Following java code is run under struts action handler ,our system is multiuser environment ,so multiple threads is created to handle the multiple user requests.
File.mkdirs() is not thread safe.
The problem is the way in which the mkdirs() function is implemented. It first tries to check whether each of the parent directories exist, and if a parent directory does not exist, then it is created using the mkdir() function.
suppose two threads try to create the following two directories at the same time
Thread1: "Test/CoreJava/Threads"
Thread2: "Test/CoreJava/Swings"
At here suppose Thread1 attempt to file.mkdirs() , and it will make the directory Test/CoreJava/, at the same time Thread2 also attempt to file.mkdirs() or vise verse.So Thread1 or Thread2 attempt will fail and give the error :Cannot create a file when that file already exists.
Following is our code...
if (!file.exists())
{
try
{
file.mkdirs();
}
catch (Exception e)
{
if (!file.exists())
{
file.mkdirs();
}
}
}
How to resolve the describe situation?pls give the solution to prevent the exception?
Can i put the file.mkdirs() code in synchronize block, if i put the synchronize block then should we get any issue in multi user web based application?
any help will be appreciate.
Thanks & Regards
Yatin Baraiya
Re: We get the exception :Cannot create a file when that file already exists
Quote:
Originally Posted by
yatin.baraiya
Can i put the file.mkdirs() code in synchronize block, if i put the synchronize block then should we get any issue in multi user web based application?
What happened when you tried?
Re: We get the exception :Cannot create a file when that file already exists
Still i have not try , but i think it create the below issue in our application
Scenario is like
User 1:upload file "text.txt" in D:/Yatin/Mydata....
User 2:Upload the file "sample.txt" in D:/Yatin/Mydata
while if i have put file.mkdirs() in the synchronize block,then if we think that both user perform the operation at the same time.
then user 2 need to wait still user 1 process is not perform ....so this is not the perfect solutions, for us. it cause the delay the performance of our application.
can you provide me other approach for the my issue?
Re: We get the exception :Cannot create a file when that file already exists
Quote:
Originally Posted by
yatin.baraiya
while if i have put file.mkdirs() in the synchronize block,then if we think that both user perform the operation at the same time.
then user 2 need to wait still user 1 process is not perform ....so this is not the perfect solutions, for us. it cause the delay the performance of our application.
Do yourself a favor and don't over analyze this. You're worrying about micro-optimization - how long does it really take to create a set of directories especially after the first time? I'm willing to be it is on the order of 10s of milliseconds. Your users can't wait a few milliseconds?
If this is your architecture then you're stuck with synchronization. Don't worry about saving a few milliseconds. Unless you can explicitly prove that this is where a bottleneck exists then you're wasting your time with something that will likely never be a performance problem.
Re: We get the exception :Cannot create a file when that file already exists
Quote:
Originally Posted by
stdunbar
Do yourself a favor and don't over analyze this. You're worrying about micro-optimization - how long does it really take to create a set of directories especially after the first time? I'm willing to be it is on the order of 10s of milliseconds. Your users can't wait a few milliseconds?
If this is your architecture then you're stuck with synchronization. Don't worry about saving a few milliseconds. Unless you can explicitly prove that this is where a bottleneck exists then you're wasting your time with something that will likely never be a performance problem.
Hy stdunbar
ok, i am not worry about micro milliseconds , now i am applying that code inside the synchronization block, but i am not sure about that, is it really solve my issue? Can you describe me about that synchronization block use in regards of my issue?
Re: We get the exception :Cannot create a file when that file already exists
I would do something like:
Code :
public static synchronized void makeDirectories( File file ) {
boolean createdDirectories = file.mkdirs();
if( !createdDirectories )
throw new IOException( "Can't create directory \"" + file.getCanonicalPath() + "\"" );
}
It doesn't have to be much more than that.