I am saving bulk data in database however it takes lots of time to save 3000 rows so I thought of clearing the session every-time I save the data.I am unable to retrieve the session object.
Error:
Stacktrace follows:
Message: Cannot invoke method getCurrentSession() on null object
Code:
def importUpload() {
def sessionFactory;
def session = sessionFactory.getCurrentSession()
User currentUser = springSecurityService.getCurrentUser();
def file = request.getFile('file')
InputStream is =file.getInputStream()
XSSFWorkbook workbook = new XSSFWorkbook(is);
int sheet_Num = workbook.getNumberOfSheets();
for (int sheetNo = 0; sheetNo < sheet_Num; sheetNo++) {
Sheet sheet = workbook.getSheetAt(sheetNo);
for (int row = 1; row < sheet.getLastRowNum(); row++) {
PrimaryOwner = sheet.getRow(row).getCell(1);
SecondaryOwner = sheet.getRow(row).getCell(2);
BusinessUnit = sheet.getRow(row).getCell(3);
ProfileTitle = sheet.getRow(row).getCell(5);
def job = Job.findByName(ProfileTitle);
if(!job){
Job jobObj=new Job();
jobObj.account = currentUser.account;
jobObj.contactName = PrimaryOwner;
jobObj.secondaryContactName = SecondaryOwner
jobObj.buisnessUnit = BusinessUnit
jobObj.name = ProfileTitle
job = jobObj.save(failOnError:true,flush:true);
session.flush()
session.clear()
}
}
Take a look at this blog: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql
I followed the guidance on it to flush and clear the session with good results. I combined that with some batch saving.
Something like this:
SessionFactory sessionFactory //inject sessionFactory
void CreateBills(List<Statement> statements){
statements.colate(100).each{List<Statement> batchedStatements ->
batchedStatements.each{Statement statement ->
new Bill(statement).save()
}
cleanupGorm()
}
}
void cleanupGorm(){
def session = sessionFactory.currentSession
session.flush()
session.clear()
DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP.get().clear()
}