我正在使用 StAX 读取我的文件,其中包含一些 Base64 数据,并使用 Hibernate 将其保存到数据库中。
XML:
<root>
<base64>lololencoded12</base64>
<base64>encodedlolos32</base64>
...............................
</root>
要读取和保存的代码:
xmlif = (XMLInputFactory2) XMLInputFactory2.newInstance();
xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
xmlif.configureForLowMemUsage();
List<Entity> entities = new ArrayList();
FileInputStream fis = new FileInputStream(filename);
XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(filename, fis);
int eventType = xmlr.getEventType();
String curElement = "";
while (xmlr.hasNext()) {
eventType = xmlr.next();
switch (eventType) {
case XMLEvent.START_ELEMENT:
curElement=xmlr.getName().toString();
if ("base64".equals(curElement)) {
Entity entity = new Entity();
entity.setBase64(xmlr.getElementText().getBytes());
session.save(entity);
session.flush();
}
break;
}
}
iterator itr = entities.iterator();
while (itr.hasNext()) {
Entity e = (Entity)itr.next();
session.saveOrUpdate(e);
}
这种方法消耗的内存量是我的 xml 大小的 6-9 倍。我该如何改进?
编辑
如果我注释掉entity.setBase64(),一切都很好。将 byte[] 保存到数据库时,内存使用量会变得疯狂。为什么?
编辑 实体 getter 和 setter :
//for me
public byte[] getBase64() {
return base64;
}
public void setBase64(byte[] base64) {
this.base64= base64;
}
//for hibernate
public Blob getBase64Blob() {
if (this.base64!=null) {
LobCreator lobok =Hibernate.getLobCreator(MainFrame.sessionFactory.getCurrentSession());
return lobok.createBlob(base64);
} else {
return null;
}
}
public void setBase64Blob(Blob dataBlob) {
if (dataBlob!=null) {
this.base64= toByteArray(dataBlob);
}
}
//utilities methods from blob to byte array
private byte[] toByteArray(Blob fromBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromBlob, baos);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException ex) {
}
}
}
}
private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
}
return baos.toByteArray();
}
编辑 xmlr.getElementText().getBytes() 由于某种原因导致大量内存使用。
最佳答案
您多久刷新一次实体?
我认为您构建了太多实体,并且没有足够频繁地刷新 session ,这会导致创建的所有这些对象都保留在当前 session 中。
可能想检查一下。
编辑
下面的评论表明提交者想要一种将二进制 blob 数据直接插入数据库的方法。
无需使用 hibernate 来完成此操作,只需使用 JDBC 即可实现。
java.sql.Connection conn = ...
java.sql.PreparedStatement pstmt= conn.prepareStatement("insert into ENTITY_TABLE (BASE64) VALUES (?)");
InputStream is= ... // byte data
pstmt.setBinaryStream(1, is);
int numUpdated= pstmt.executeUpdate();
现在请注意,这真的很困惑。这是假设 ENTITY_TABLE 使用数据库生成的行标识符,并且允许除 BASE64 之外的列具有空值或具有合理的默认值。 executeUpdate 将运行插入语句,将 is 的值作为 blob 数据的值。
我希望这能让您更接近您的解决方案。
关于java - StAX - 将 Base64 字符串从 xml 读取到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512646/