Skip to content
Snippets Groups Projects
Commit e6c63da0 authored by buczekp's avatar buczekp
Browse files

[SE-154] fixed race condition - 1v

SVN: 12993
parent 45eab0e0
No related branches found
No related tags found
No related merge requests found
...@@ -89,20 +89,27 @@ public class DBUtils ...@@ -89,20 +89,27 @@ public class DBUtils
* Creates the data set based on the information given in <var>dataSet</var>. The sample and * Creates the data set based on the information given in <var>dataSet</var>. The sample and
* experiment of the data set may already exist in the database. If they don't, they are created * experiment of the data set may already exist in the database. If they don't, they are created
* as well. * as well.
* <p>
* NOTE: Code responsible for trying to get sample and experiment from the DB and creating them
* if they don't exist is in synchronized block and uses currently opened transaction. Then the
* transaction is closed and data set is added to the DB in second transaction. If second
* transaction will be rolled back sample and experiment created in first transaction will stay
* in the DB.
*/ */
public static void createDataSet(IGenericDAO dao, DMDataSetDTO dataSet) public static void createDataSet(IGenericDAO dao, DMDataSetDTO dataSet)
{ {
DMExperimentDTO experiment = getOrCreateExperiment(dao, dataSet); synchronized (IGenericDAO.class)
dataSet.setExperimentId(experiment.getId()); // make sure all the ids are set correctly.
if (dataSet.getSample() != null)
{ {
String permId = dataSet.getSample().getPermId(); DMExperimentDTO experiment = getOrCreateExperiment(dao, dataSet);
DMSampleDTO sample; dataSet.setExperimentId(experiment.getId()); // make sure all the ids are set correctly.
// it may have happened that the sample has been created by another thread after
// we checked that it does not exist if (dataSet.getSample() != null)
synchronized (IGenericDAO.class)
{ {
String permId = dataSet.getSample().getPermId();
DMSampleDTO sample;
// it may have happened that the sample has been created by another thread after
// we checked that it does not exist
sample = dao.getSampleByPermId(permId); sample = dao.getSampleByPermId(permId);
if (sample == null) if (sample == null)
{ {
...@@ -110,10 +117,13 @@ public class DBUtils ...@@ -110,10 +117,13 @@ public class DBUtils
sample.setExperiment(experiment); sample.setExperiment(experiment);
sample = createSample(dao, sample, permId); sample = createSample(dao, sample, permId);
} }
sample.setExperiment(experiment);
dataSet.setSample(sample); // make sure all the ids are set correctly.
} }
sample.setExperiment(experiment); dao.close(true);
dataSet.setSample(sample); // make sure all the ids are set correctly.
} }
long dataSetId = dao.addDataSet(dataSet); long dataSetId = dao.addDataSet(dataSet);
dataSet.setId(dataSetId); dataSet.setId(dataSetId);
} }
......
...@@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException; ...@@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException;
import net.lemnik.eodsql.QueryTool; import net.lemnik.eodsql.QueryTool;
import net.lemnik.eodsql.TransactionQuery; import net.lemnik.eodsql.TransactionQuery;
import org.springframework.dao.DataAccessException;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
...@@ -100,7 +101,13 @@ public class EICML2Database ...@@ -100,7 +101,13 @@ public class EICML2Database
transaction.close(true); transaction.close(true);
} catch (Throwable th) } catch (Throwable th)
{ {
DBUtils.rollbackAndClose(transaction); try
{
DBUtils.rollbackAndClose(transaction);
} catch (DataAccessException ex)
{
// Avoid this exception shadowing the original exception.
}
throw CheckedExceptionTunnel.wrapIfNecessary(th); throw CheckedExceptionTunnel.wrapIfNecessary(th);
} }
} }
...@@ -115,8 +122,8 @@ public class EICML2Database ...@@ -115,8 +122,8 @@ public class EICML2Database
int permId = 0; int permId = 0;
for (String f : new File(dir).list(new EICMLFilenameFilter())) for (String f : new File(dir).list(new EICMLFilenameFilter()))
{ {
eicML2Database.uploadEicMLFile(new File(dir, f), new DMDataSetDTO( eicML2Database.uploadEicMLFile(new File(dir, f), new DMDataSetDTO(Integer
Integer.toString(++permId), "sample1", "the sample name", "experiment1", .toString(++permId), "sample1", "the sample name", "experiment1",
"the experiment name")); "the experiment name"));
} }
System.out.println((System.currentTimeMillis() - start) / 1000.0); System.out.println((System.currentTimeMillis() - start) / 1000.0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment