gpt4 book ai didi

grails - 如何提高在grails中保存批量数据的性能?

转载 作者:行者123 更新时间:2023-12-02 14:00:49 24 4
gpt4 key购买 nike

我正在将大量数据保存在数据库中,但是保存3000行需要花费大量时间,因此我想每次保存数据时都要清除 session 。我无法检索 session 对象。

错误:

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

我按照它的指导进行冲洗并清除了 session ,并取得了良好的效果。我结合了一些批量保存。

像这样:

    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()
}

关于grails - 如何提高在grails中保存批量数据的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com