Oracle 10g,使用Java进行PDF创建会导致打开文件过多

时间:2014-02-07 08:05:08

标签: java database windows oracle file

执行程序后大约一个月的常规工作负载系统命中错误。 ora 29532 java.io.filenotfoundexception太多打开的文件。

我相信我会关闭我打开它们的句柄。

操作系统:Win2003 x64 Oracle 10.0.2.1 Database EE

  

java.io.FileNotFoundException:打开的文件过多       at java.io.FileInputStream.open(Native Method)       在java.io.FileInputStream。(FileInputStream.java:106)       在java.io.FileInputStream。(FileInputStream.java:66)       at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:69)       at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:156)       在java.net.URL.openStream(URL.java:924)       at oracle.xml.parser.v2.XMLReader.openURL(XMLReader.java:2366)       at oracle.xml.parser.v2.XMLReader.pushXMLReader(XMLReader.java:279)       at oracle.xml.parser.v2.NonValidatingParser.pushExternalDTD(NonValidatingParser.java:542)       at oracle.xml.parser.v2.NonValidatingParser.parseDoctypeDecl(NonValidatingParser.java:479)       at oracle.xml.parser.v2.NonValidatingParser.parseProlog(NonValidatingParser.java:311)       at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:290)       at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:200)       at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:155)       at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:111)       在domain.pdf.java_html_to_pdf.print_PDF(JAVA_HTML_TO_PDF:71)       在domain.pdf.java_html_to_pdf.convetr_html_to_pdf(JAVA_HTML_TO_PDF:135)       打开的文件太多

create or replace and compile java source named WEB.java_html_to_pdf as
package domain.pdf;

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;

import oracle.sql.BLOB;
import oracle.sql.CLOB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;

import org.w3c.tidy.Tidy;
import org.w3c.tidy.Configuration;

// --------------------------------------------------------------------------------------
public class java_html_to_pdf
{
  public static int ERR_PDF_PRINTING = 10;
  public static int ERR_IO_PROCESSES = 20;
  public static int ERR_VALIDATING = 30;
  protected static int error = 0;

  public static byte[] normalise_html(byte [] inptByteArray)
  {
    ByteArrayOutputStream fos = new ByteArrayOutputStream();
        try
        {
          ByteArrayInputStream fis = new ByteArrayInputStream(inptByteArray);
            Tidy tidy = new Tidy();
            tidy.setXHTML(true);
            tidy.setTidyMark(false);
            tidy.setMakeClean(true);
            tidy.setCharEncoding(Configuration.UTF8);
            tidy.parse(fis, fos);
            fis.close();

            fos.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
            error = ERR_VALIDATING;
            return null;
        }
        return fos.toByteArray();
  }

  public static byte[] print_PDF(byte[] inpt)
  //public static byte[] print_PDF(InputStream inpt)
  {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try
    {
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      db.setEntityResolver(new md.cadastre.pdf.entity_resolver());
      Document d = db.parse(new ByteArrayInputStream(inpt));
      //Document d = db.parse(inpt);
        ITextRenderer renderer = new ITextRenderer();
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIAL.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALBD.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALBI.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALI.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALCE.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALCEBD.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALCEBI.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.getFontResolver().addFont("D:\\oracle\\HTML_TO_PDF\\FONTS\\ARIALCEI.TTF", BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        renderer.setDocument(d, null);
        renderer.layout();
        renderer.createPDF(os, false);
        renderer.finishPDF();
        os.close();
        //os = null;  ???????
    }
      catch (Exception e)
      {
        e.printStackTrace();
        System.err.println(e.getMessage());
        error = ERR_PDF_PRINTING;
        return null;
      }
      return os.toByteArray();
  }

  private static  Connection getConnection() throws SQLException
  {
    return DriverManager.getConnection("jdbc:default:connection:");
  }


    public static BLOB convetr_html_to_pdf(CLOB clob)
    {
        Writer writer = null;
        Reader reader = null;
    Connection con = null;
    BLOB blob = null;
    OutputStream out = null;
    InputStream in = null;
        ByteArrayOutputStream ostr = null;
        byte[] tmp;
        try
        {
            ostr = new ByteArrayOutputStream();
            writer = new BufferedWriter(new OutputStreamWriter(ostr));
            reader = new BufferedReader(clob.getCharacterStream());
            int length;
            char[] cbuf = new char[clob.getChunkSize()];
            while ((length = reader.read(cbuf, 0, clob.getChunkSize())) != -1)
            {
                writer.write(cbuf, 0, length);
            }
            writer.close();
            reader.close();
            tmp = ostr.toByteArray();
            // for validating input HTML doc uncomment next lines
            tmp = normalise_html(tmp);
            if (error != 0)
            {
              return null;
            }
            tmp = print_PDF(tmp);
            if (error != 0)
            {
              return null;
            }

      con = getConnection();
      blob = BLOB.createTemporary(con, true, BLOB.DURATION_SESSION);
      out = blob.getBinaryOutputStream();
      in = new BufferedInputStream(new ByteArrayInputStream(tmp));
      int chunkSize = blob.getChunkSize();
      byte[] bbuf = new byte[chunkSize];
      while ((length = in.read(bbuf)) != -1)
      {
        out.write(bbuf, 0, length);
      }
      if (in != null) {in.close();}
      if (out != null) {out.close();}
      if (con != null) {con.close();}
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.err.println(e.getMessage());
            return null;
        }
        return blob;
    }
}

虽然Linux / Unix建议增加句柄数是很常见的,但在Microsoft Windows环境中我认为它的上限为16k。

我找不到来自太多文件的罪魁祸首。

1 个答案:

答案 0 :(得分:0)

您是否检查过日志中的例外情况?我怀疑你在遇到异常时有未关闭的对象,因为你没有在finally块中执行资源关闭。