java - 使用 Saxp 读取和 hibernate 保存大型数据库时 Netbean 没有响应

标签 java hibernate saxparser netbeans-7

我的项目解析来自 dblp 的 xml 文件,它在 ORM 数据库中保存大约 1Gb,所以我使用 SAXP 读取信息,如(论文'作者,论文'信息......)并使用 Hibeanate 将数据保存到数据库中:

第 1 步:使用 SAXP 读取这样的元素:

<article key="journals/cs/BhaskarAS11" mdate="2011-11-09"><author>Data Ram Bhaskar</author><author>Kasim Karam Abdalla</author><author>Raj Senani</author><title>Electronically-Controlled Current-mode second order Sinusoidal Oscillators Using MO-OTAs and Grounded Capacitors.</title><pages>65-73</pages><year>2011</year><volume>2</volume><journal>Circuits and Systems</journal><number>2</number><ee>http://dx.doi.org/10.4236/cs.2011.22011</ee><url>db/journals/cs/cs2.html#BhaskarAS11</url></article>

要获取:作者姓名、出版物、出版商、……出版物

第 2 步:检查数据库中重复的作者、出版商...。 第 3 步:将出版物保存在数据库中 结束:转到下一个元素,直到结束数据库。

当我运行大约 10MB 的文件 dblp 时,它运行正常(11 分钟)- 它向数据库输入了大约 21000 个 publicaiton。但是当运行完整的 dblp 文件 (1GB) 时,当在数据库中看到它大约 75000 pub(大约 1 天)时,Netbean 抛出不响应 - 我无法从 netBean 获得任何错误或消息 - 它无法输入任何 pub进入数据库(但在 dblp 中有大约 170 万个酒吧)。

很抱歉问了这么长的问题,但我非常感谢你们能给我的任何帮助! SAXP 的问题?还是 hibernate ……?

这里是一些代码:

hibernate 配置:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cspublicationcrawler</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.transaction.auto_close_session">false</property>
    <property name="hibernate.transaction.flush_before_completion">true</property>

    <property name="hibernate.connection.pool_size">50</property>
    <property name ="hibernate.jdbc.batch_size">500</property> 
    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Magazine.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Reviewer.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Conference.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Author.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Paper.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/RankAuthorKeyword.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Publisher.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Comment.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Domain.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/Journal.hbm.xml"/>
    <mapping resource="uit/tkorg/cspublicationtool/entities/PaperType.hbm.xml"/>
  </session-factory>

hibernate 工具

public HibernateUtil() throws Exception {
    sessionFactoryConfigPath = "";
    sessionFactory = new Configuration().configure().buildSessionFactory();
}

public HibernateUtil(String sessionFactoryConfigPath) {
    this.sessionFactoryConfigPath = sessionFactoryConfigPath;
    sessionFactory = new Configuration().configure(sessionFactoryConfigPath).buildSessionFactory();
}

/**
 * Begin a transaction
 */
protected void beginTransaction() {
    session = sessionFactory.getCurrentSession();
    session.beginTransaction();
}

/**
 * Commit transaction and close session
 */
protected void commitAndClose() {
    if (session != null) {
        for(int i=0;i<10000;i++) {
            if ( i % 500 == 0 ) { //50, same as the JDBC batch size
                                 //flush a batch of inserts and release memory:
                    session.flush();
                    session.clear();
            }               
        }
        session.getTransaction().commit();
        if (session.isOpen()) {
            session.close();
        }
    }
}

SAXP

  public void endElement(String uri, String localName, String qName) throws SAXException {
    try {
        if(!recordTag.equals(WWW)&&!recordTag.equals(PROCEEDINGS)){
            super.endElement(uri, localName, qName);
            if(this.str != null){
                this.value = this.str.toString();
            }
            if (qName.equals(AUTHOR) || qName.equals(EDITOR)) {
                 try {                        
                    String temp = value.replaceAll("'","");
                    author = this.authorBO.checkExitAuthor(temp);
                    if (author ==null)
                    {
                        author = new Author();
                        author.setAuthorName(value);
                        authorBO.addNew(author);
                    }
                     authors.add(author);
                     author=null;
                return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(qName.equals(TITLE)){
                this.paper.setTitle(value);
                return;
            }

            if(qName.equals(BOOKTITLE)){
                if(recordTag.equals(INPROCEEDINGS)){
                    String temp = value.replaceAll("'","");
                    conference = conferenceBO.checkExitConference(temp);
                    if(conference == null)
                    {
                        conference = new Conference();
                        conference.setConferenceName(value);
                        conferenceBO.addNew(conference);
                        this.paper.setConference(conference);
                        conference=null;
                        return;
                    }
                }else {
                    this.paper.setTitle(value);
                    return;
                }
            }

            if(qName.equals(PAGES)){
                this.paper.setPages(value);
                return;
            }

            if(qName.equals(YEAR)){
                this.paper.setYear(Integer.parseInt(value));
                return;
            }

            if(qName.equals(ADDRESS)){
                this.paper.setAdress(value);
                return;
            }
            if(qName.equals(JOURNAL)){
                try {
                    String temp = value.replaceAll("'","");
                    journal = this.journalBO.checkExitJournal(temp);
                    if (journal ==null)
                    {
                        journal = new Journal();
                        journal.setJournalName(value);
                        journalBO.addNew(journal);
                    }

                    this.paper.setJournal(journal);
                    journal =null;                        
                    return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(qName.equals(VOLUME)){
                this.paper.setVolume(value);
                return;
            }

            if(qName.equals(NUMBER)){
                this.paper.setNumber(value);
                return;
            }

            if(qName.equals(MONTH)){
                this.paper.setMonth(value);
                return;
            }

            if(qName.equals(URL)){
                this.paper.setUrl(value);
                return;
            }

            if(qName.equals(EE)){
                this.paper.setEe(value);
                return;
            }

            if(qName.equals(CDROM)){
                this.paper.setCdrom(value);
                return;
            }

            if(qName.equals(CITE)){
                this.paper.setCite(value);
                return;
            }

            if(qName.equals(PUBLISHER)){
                 try {
                    String temp = value.replaceAll("'","");
                    publisher =publisherBO.checkExitPublisher(temp);
                    if (publisher ==null)
                    {
                        publisher = new Publisher();
                        publisher.setNamePublisher(value);
                        publisherBO.addNew(publisher);
                    }

                    this.paper.setPublisher(publisher);
                    publisher=null;                        
                    return;
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(qName.equals(CROSSREF)){
                this.paper.setCrossref(value);
                return;
            }

            if(qName.equals(ISBN)){
                this.paper.setIsbn(value);
                return;
            }

            if(qName.equals(SERIES)){
                this.paper.setSeries(value);
                return;
            }

            if(qName.equals(SCHOOL)){
                this.paper.setSchool(value);
                return;
            }

            if(qName.equals(CHAPTER)){
                this.paper.setChapter(value);
                return;
            }

            if (qName.equals(recordTag)) {
                this.paper.setAuthors(authors);
                this.paperBO.addNew(paper);
                if(this.authors != null){
                    this.authors = null;
                }
                if(this.paper != null){
                    this.paper = null;
                }
                if(this.str != null){
                    this.str = null;
                }
                return;
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void endDocument() throws SAXException {
    super.endDocument();
}

@Override
public void startDocument() throws SAXException {
    super.startDocument();
    try {
        str = new StringBuffer();
        this.authorBO = AuthorBO.getAuthorBO();
        this.conferenceBO = ConferenceBO.getConferenceBO();
        this.journalBO = JournalBO.getJournalBO();
        this.publisherBO = PublisherBO.getPublisherBO();
        this.paperTypeBO = PaperTypeBO.getPaperTypeBO();
        this.paperBO = PaperBO.getPaperBO();
    } catch (Exception ex) {
        Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    super.startElement(uri, localName, qName, attributes);    
    this.str = new StringBuffer();
    if ((attributes.getLength()>0) && (attributes.getValue("key")!=null)) {
        recordTag = qName;
        this.paper = new Paper();            
        this.authors = new HashSet <Author>();       
        this.paper.setDblpKey(attributes.getValue("key"));            
        if(!recordTag.equals(WWW)&&!recordTag.equals(PROCEEDINGS))
        {
            papertype = this.paperTypeBO.checkExitPaperType(qName);
            if (papertype ==null)
            {
                try {
                    papertype = new PaperType();
                    papertype.setNameType(qName);
                    paperTypeBO.addNew(papertype);
                } catch (Exception ex) {
                    Logger.getLogger(CSPublicationSAXEventHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.paper.setPaperType(papertype);
            papertype=null;
        }
        return;
    }
} 

最佳答案

netbeans(或任何其他运行代码的工具)崩溃的原因很简单:在大型数据集上,hibernate 的有状态 session 会占用大量内存。

解决方案:使用“无状态” hibernate session - 您应该为大型 hibernate ETL 作业使用无状态 session (它不跟踪对象的状态)。

//edit the session creation method in HibernateUtil as follows.
    StatelessSession session = sessionFactory.openStatelessSession();

为什么“StatelessSessions”对于 ETL 风格的 hibernate 作业很重要:由于典型的 JVM 运行时内存小于 1g,因此在通过 hibernate 。

其他优化:

1) 在性能极高的服务器或数据库服务器本地的服务器上运行您的 hibernate 程序。

2) 在导入程序运行时禁用数据库服务器中的任何外键检查和索引。

3) 查看其他 hibernate java/GC 优化,例如,减少垃圾收集任务或增加 JVM 内存。 Java Application Server Performance

为了解决这个问题,您需要更改 session 类型 - 并对插入、提交和刷新的方式进行一些小的修改。

为什么我们使用不同的 session 类型:

当 hibernate 变慢或停止响应时,这可能意味着您也在插入 JVm 内存方面的限制(也就是说,您已经创建了许多在 JVM 内存中管理的对象)。 Hibernate 出色的 web 应用程序性能是由于它能够管理它在数据库中查询或更新的 bean 的状态和生命周期——通常,我们在标准 web 应用程序中从中获益,而没有意识到隐藏的成本。因此,当我们进行大型 ETL 风格的数据库编程时,我们不需要缓存,不需要状态——只需要高速写入。这意味着使用无状态 session ,它不会尝试更高级的、网络优化的 hibernate 状态 session 管理。

关于java - 使用 Saxp 读取和 hibernate 保存大型数据库时 Netbean 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556450/

相关文章:

java - 如何在 Java 中调用这个 PLSQL 过程?

c++ - 在 libXML sax 解析器(C++)中获取属性值的正确方法是什么?

Java 使用 Saxparser 导致抛出异常

java - 如何使用条件在 Hibernate 中实现选择和计数

java - 如何: Hibernate 3. xx条件查询

java - 获取 XSD 验证错误的父元素

java - 自定义字体单选按钮未检测到单击

java - 使用 servlet 将动态图像传递到 JSP

java - Cassandra 卡在 Initializing IndexInfo 上

java - 使用 Graphics2D 填充透明度