Skip to content

Commit 32366d3

Browse files
authored
Fix Serializable issues (#132)
1 parent dc8f6af commit 32366d3

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/integrationTest/java/com/mongodb/hibernate/TestCommandListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class TestCommandListener implements CommandListener, Service {
3030

3131
static TestCommandListener INSTANCE = new TestCommandListener();
3232

33-
private final List<BsonDocument> startedCommands = new ArrayList<>();
33+
private final transient List<BsonDocument> startedCommands = new ArrayList<>();
3434

3535
private TestCommandListener() {}
3636

src/main/java/com/mongodb/hibernate/internal/extension/service/StandardServiceRegistryScopedState.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.mongodb.hibernate.internal.cfg.MongoConfiguration;
2525
import com.mongodb.hibernate.internal.cfg.MongoConfigurationBuilder;
2626
import com.mongodb.hibernate.service.spi.MongoConfigurationContributor;
27+
import java.io.IOException;
28+
import java.io.NotSerializableException;
29+
import java.io.ObjectOutputStream;
2730
import java.io.Serial;
2831
import java.util.Map;
2932
import org.hibernate.HibernateException;
@@ -38,7 +41,7 @@ public final class StandardServiceRegistryScopedState implements Service {
3841
@Serial
3942
private static final long serialVersionUID = 1L;
4043

41-
private final MongoConfiguration config;
44+
private final transient MongoConfiguration config;
4245

4346
@VisibleForTesting(otherwise = PRIVATE)
4447
public StandardServiceRegistryScopedState(MongoConfiguration config) {
@@ -49,6 +52,12 @@ public MongoConfiguration getConfiguration() {
4952
return config;
5053
}
5154

55+
@Serial
56+
private void writeObject(ObjectOutputStream out) throws IOException {
57+
throw new NotSerializableException(
58+
"This class is not designed to be serialized despite it having to implement `Serializable`");
59+
}
60+
5261
public static final class ServiceContributor implements org.hibernate.service.spi.ServiceContributor {
5362
public ServiceContributor() {}
5463

src/main/java/com/mongodb/hibernate/internal/type/MongoStructJdbcType.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import static com.mongodb.hibernate.internal.type.ValueConversions.toDomainValue;
2828

2929
import com.mongodb.hibernate.internal.FeatureNotSupportedException;
30+
import java.io.IOException;
31+
import java.io.NotSerializableException;
32+
import java.io.ObjectOutputStream;
3033
import java.io.Serial;
3134
import java.sql.CallableStatement;
3235
import java.sql.Connection;
@@ -58,7 +61,7 @@ public final class MongoStructJdbcType implements StructJdbcType {
5861
public static final MongoStructJdbcType INSTANCE = new MongoStructJdbcType();
5962
public static final JDBCType JDBC_TYPE = JDBCType.STRUCT;
6063

61-
private final @Nullable EmbeddableMappingType embeddableMappingType;
64+
private final transient @Nullable EmbeddableMappingType embeddableMappingType;
6265
private final @Nullable String structTypeName;
6366

6467
private MongoStructJdbcType() {
@@ -206,6 +209,12 @@ public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
206209
return new Extractor<>(javaType);
207210
}
208211

212+
@Serial
213+
private void writeObject(ObjectOutputStream out) throws IOException {
214+
throw new NotSerializableException(
215+
"This class is not designed to be serialized despite it having to implement `Serializable`");
216+
}
217+
209218
/** Thread-safe. */
210219
private final class Binder<X> extends BasicBinder<X> {
211220
@Serial

src/main/java/com/mongodb/hibernate/jdbc/MongoConnectionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public final class MongoConnectionProvider implements ConnectionProvider, Stoppa
5656
private static final long serialVersionUID = 1L;
5757

5858
private @Nullable StandardServiceRegistryScopedState standardServiceRegistryScopedState;
59-
private @Nullable MongoClient mongoClient;
59+
private transient @Nullable MongoClient mongoClient;
6060

6161
@Override
6262
public Connection getConnection() throws SQLException {

0 commit comments

Comments
 (0)