Witam,
próbuje zapisać do bazy plik (około 5MB).
Tabela ma postać:
CREATE TABLE pliki (
name VARCHAR(30) NOT NULL PRIMARY KEY,
file LONGBLOB NOT NULL,
);
Niestety otrzymuje wyjątek:
java.sql.SQLException: Packet for query is too large (10752083 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
Nie chcę zmieniać rozmiaru dozwolnego pakietu, więc postanowiłem rozwiązać to w ten sposób:
int SIZE = 10240;
String insert = "INSERT INTO pliki (name, file) VALUES (?,?)";
//Plik przekazywany jest jako: byte[] file
PreparedStatement stmt = null
if(file.length < SIZE) {
stmt = conn.getConnection().prepareStatement(insert);
pstmt.setString(1, fileName);
pstmt.setBytes(2, file);
pstmt.execute();
pstmt.clearParameters();
conn.commit();
} else {
byte[] buffer = new byte[SIZE];
System.arraycopy(file, 0, buffer, 0, buffer.length);
pstmt = conn.getConnection().prepareStatement(insert);
pstmt.setString(1, fileName);
pstmt.setBytes(2, buffer);
pstmt.execute();
pstmt.clearParameters();
conn.commit();
String append = "UPDATE pliki SET file=CONCAT(file,?) WHERE name=?";
pstmt = conn.prepareStatement(append);
for(int i=SIZE; i<file.length; i+=SIZE) {
buffer = new byte[ (SIZE < (file.length - i)) ? SIZE : Math.abs(file.length - i) ];
System.arraycopy(file, i, buffer, 0, buffer.length);
pstmt.setBytes(1, buffer);
pstmt.setString(2, fileName);
pstmt.execute();
pstmt.clearParameters();
}
conn.getConnection().commit();
}
Wywala wyjątek:
java.sql.SQLException: Result of concat() was larger than max_allowed_packet (1048576) - truncated
Zacząłem zmieniać rozmiar zmiennej SIZE, na coraz mniejszy 8192, 4048, 2024... bez rezultatu. (ciągle ten sam wyjątek).
korzystam z MySQL w wersji: 5.0.51b-community-nt,
biblioteka JDBC: mysql-connector-java-5.0.8
Zastanawia mnie co robię źle, lub jak można to rozwiązać w inny sposób.