Skip to content

Commit d96bac3

Browse files
author
Ruslan Gainutdinov
committed
Fixed rollback/commit cycle
1 parent 67fe17a commit d96bac3

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

src/main/java/com/wizecore/graylog2/plugin/JDBCOutput.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private void reconnect() throws SQLException {
7979

8080
logInsert = connection.prepareStatement("insert into log (message_date, message_id, source, message) values (?, ?, ?, ?)");
8181
logInsertAttribute = connection.prepareStatement("insert into log_attribute (message_id, name, value) values (?, ?, ?)");
82+
connection.setAutoCommit(false);
8283
}
8384

8485
@Override
@@ -136,40 +137,44 @@ public void write(Message msg) throws Exception {
136137
reconnect();
137138
}
138139

139-
connection.setAutoCommit(false);
140-
try {
141-
int index = 1;
142-
logInsert.setTimestamp(index++, new Timestamp(msg.getTimestamp().getMillis()));
143-
logInsert.setString(index++, msg.getId());
144-
logInsert.setString(index++, msg.getSource());
145-
logInsert.setString(index++, msg.getMessage());
146-
logInsert.execute();
147-
Object id = null;
148-
ResultSet ids = logInsert.getGeneratedKeys();
149-
while (ids != null && ids.next()) {
150-
id = ids.getObject(1);
151-
}
152-
if (id != null) {
153-
for (Entry<String, Object> e: msg.getFieldsEntries()) {
154-
String name = e.getKey();
155-
Object value = e.getValue();
156-
logInsertAttribute.setObject(1, id);
157-
logInsertAttribute.setString(2, name);
158-
logInsertAttribute.setObject(3, value);
159-
logInsertAttribute.execute();
160-
}
161-
} else {
162-
throw new SQLException("Failed to generate ID for primary log record!");
163-
}
164-
} finally {
165-
connection.rollback();
166-
connection.commit();
167-
connection.setAutoCommit(true);
168-
}
140+
int index = 1;
141+
logInsert.setTimestamp(index++, new Timestamp(msg.getTimestamp().getMillis()));
142+
logInsert.setString(index++, msg.getId());
143+
logInsert.setString(index++, msg.getSource());
144+
logInsert.setString(index++, msg.getMessage());
145+
logInsert.execute();
146+
Object id = null;
147+
ResultSet ids = logInsert.getGeneratedKeys();
148+
while (ids != null && ids.next()) {
149+
id = ids.getObject(1);
150+
}
151+
if (id != null) {
152+
for (Entry<String, Object> e: msg.getFieldsEntries()) {
153+
String name = e.getKey();
154+
Object value = e.getValue();
155+
String s = value != null ? value.toString() : null;
156+
logInsertAttribute.setObject(1, id);
157+
logInsertAttribute.setString(2, name);
158+
logInsertAttribute.setString(3, s);
159+
logInsertAttribute.execute();
160+
}
161+
} else {
162+
throw new SQLException("Failed to generate ID for primary log record!");
163+
}
169164
} catch (SQLException e) {
170165
log.log(Level.WARNING, "JDBC output error: " + e.getMessage(), e);
166+
try {
167+
connection.rollback();
168+
connection.setAutoCommit(true);
169+
} catch (SQLException ee) {
170+
// Don`t care
171+
}
171172
connection = null;
172-
}
173+
} finally {
174+
if (connection != null) {
175+
connection.commit();
176+
}
177+
}
173178
}
174179

175180
public interface Factory extends MessageOutput.Factory<JDBCOutput> {

0 commit comments

Comments
 (0)