如何提高在 Grails 中保存批量数据的性能?
How to improve performance of saving bulk data in grails?
我正在数据库中保存大量数据,但是保存 3000 行需要很多时间,所以我想在每次保存时清除会话 data.I 我无法检索会话对象。
错误:
Stacktrace follows:
Message: Cannot invoke method getCurrentSession() on null object
代码:
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()
}
}
看看这个博客:http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql
我按照它的指导刷新并清除了会话,并取得了良好的效果。我将其与一些批量保存相结合。
像这样:
<pre>
SessionFactory sessionFactory //inject sessionFactory</p>
<pre><code>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()
}
我正在数据库中保存大量数据,但是保存 3000 行需要很多时间,所以我想在每次保存时清除会话 data.I 我无法检索会话对象。
错误:
Stacktrace follows:
Message: Cannot invoke method getCurrentSession() on null object
代码:
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()
}
}
看看这个博客:http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql
我按照它的指导刷新并清除了会话,并取得了良好的效果。我将其与一些批量保存相结合。
像这样:
<pre>
SessionFactory sessionFactory //inject sessionFactory</p>
<pre><code>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()
}