如何在没有OutOfMemoryError的情况下在JDBCMySQL中插入一个300MB的blob?
我想在mysql中插入一个300MB的blob,但我似乎做不到。我总是从记忆错误中解脱出来 我已经定好了如何在没有OutOfMemoryError的情况下在JDBCMySQL中插入一个300MB的blob?,jdbc,blob,Jdbc,Blob,我想在mysql中插入一个300MB的blob,但我似乎做不到。我总是从记忆错误中解脱出来 我已经定好了 max\u allowed\u packet=500M在my.cn中 这是我的密码: @Test public void testWriteBlobWithJdbc() throws IOException { System.out.println("Memory is " + Runtime.getRuntime().maxMemory() / 1024 / 102
max\u allowed\u packet=500M
在my.cn中
这是我的密码:
@Test
public void testWriteBlobWithJdbc() throws IOException {
System.out.println("Memory is " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " MB");
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/lazy_lob?user=root&password=itismeDA02071986");
PreparedStatement preparedStatement = connection.prepareStatement("insert into addresses(description) values(?);");
Blob blob = connection.createBlob();
BufferedOutputStream bos = new BufferedOutputStream(blob.setBinaryStream(1));
bos.write(get300MBData());
bos.flush();
bos.close();
preparedStatement.setBlob(1, blob);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
}
}
}
以下是我得到的:
Memory is 910 MB
java.lang.OutOfMemoryError: Java heap space
at com.mysql.jdbc.Blob.getBytes(Blob.java:132)
at com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java:2934)
at cgad.learning.hibernate.TestLazyLoadLob.testWriteBlobWithJdbc(TestLazyLoadLob.java:64)
所以基本上我有910MB的RAM,但我不能插入一个300MB的blob?
此外,我还以为这应该将blob流式传输到db中,但它似乎会加载到内存中,并可能导致一些问题
你能帮帮忙吗?如果你和BLOB一起工作过,我有点没有俱乐部
非常感谢您不要一次将整个300MB存储在一个巨大的字节数组中。一次一块地将其写入blob流 Blob类不会对Blob进行流式处理。不是流式传输的实体是您