Copie fichier impossible car fichier utilisé
Pour copier un fichier j'utilise la méthode suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
public static synchronized void copieFichier(File source, File destination)
throws IOException
{
RandomAccessFile fileSource = null;
FileChannel fcSource = null;
FileLock verrouSource = null;
FileOutputStream fileDestination = null;
FileChannel fcDestination = null;
FileLock verrouDestination = null;
if (!source.isFile()) { throw new IOException(); }
try
{
fileSource = new RandomAccessFile(source, "rw");
fcSource = fileSource.getChannel();
verrouSource = fcSource.lock();
fileDestination = new FileOutputStream(destination);
fcDestination = fileDestination.getChannel();
verrouDestination = fcDestination.lock();
fcSource.transferTo(0, fcSource.size(), fcDestination);
}
finally
{
if (verrouSource != null)
{
verrouSource.release();
}
if (fcSource != null)
{
fcSource.close();
}
if (fileSource != null)
{
fileSource.close();
}
if (verrouDestination != null)
{
verrouDestination.release();
}
if (fcDestination != null)
{
fcDestination.close();
}
if (fileDestination != null)
{
fileDestination.close();
}
}
} |
Cette méthode fonctionne bien avec une application ancienne. :mrgreen:
Maintenant cette fonction ne renvoie l'exception suivante : :aie:
Code:
1 2 3 4 5 6 7 8 9 10
|
C:\Projet\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\webgravity2\BASE.FDB (Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus)
java.io.RandomAccessFile.open(Native Method)
java.io.RandomAccessFile.<init>(Unknown Source)
util.Fichier.copieFichier(Fichier.java:41)
... suite de la trace ... |
Ce fichier est une base de données firebird (via JDBC).
La différence importante avec l'ancienne application est l'utilisation maintenant d'Hibernate.
Et voici le fichier HibernateUtil :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package dao.requete;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final String HIBERNATE_FICHIER_CONFIGURATION = "hibernate.cfg.xml";
private static SessionFactory sessionFactory;
public static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();
public static Configuration cfg;
public static Session currentSession() throws HibernateException
{
Session s = threadSession.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
threadSession.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = threadSession.get();
threadSession.set(null);
if (s != null)
{
s.close();
System.out.println("close session");
}
}
public static void initConfig(String pathBase)
{
cfg = new Configuration().configure(HIBERNATE_FICHIER_CONFIGURATION);
cfg.setProperty("hibernate.connection.url", "jdbc:firebirdsql:localhost/3050:" + pathBase);
// System.out.println(cfg.getProperty("hibernate.connection.url"));
cfg.setProperty("hibernate.dialect",
"org.hibernate.dialect.FirebirdDialect");
cfg.setProperty("hibernate.cglib.use_reflection_optimizer", "false");
cfg.setProperty("hibernate.connection.autocommit", "false");
//cfg.setProperty("hibernate.transaction.auto_close_session", "true");
// Create the SessionFactory
sessionFactory = cfg.buildSessionFactory();
}
} |
Enfin avant l'appel à la fonction de copie, j'exécute la fonction HibernateUtil.closeSession();
Comment résoudre ce problème ? :?
Merci. ;)