|
1 | 1 | package com.fasterxml.jackson.datatype.hibernate5;
|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
| 4 | +import java.lang.reflect.Method; |
4 | 5 | import java.util.ArrayList;
|
5 | 6 | import java.util.Collection;
|
6 | 7 | import java.util.HashMap;
|
@@ -340,9 +341,7 @@ private void initializeCollection(PersistentCollection coll, Session session) {
|
340 | 341 | // .getTransactionFactory()
|
341 | 342 | // .compatibleWithJtaSynchronization();
|
342 | 343 | //Above is removed after Hibernate 5
|
343 |
| - boolean isJTA = ((SessionImplementor) session).getTransactionCoordinator() |
344 |
| - .getTransactionCoordinatorBuilder() |
345 |
| - .isJta(); |
| 344 | + boolean isJTA = SessionReader.isJTA(session); |
346 | 345 |
|
347 | 346 | if (!isJTA) {
|
348 | 347 | session.beginTransaction();
|
@@ -422,4 +421,55 @@ private Object convertToMap(Map<?, ?> value) {
|
422 | 421 | private Object convertToSet(Set<?> value) {
|
423 | 422 | return new HashSet<>(value);
|
424 | 423 | }
|
| 424 | + |
| 425 | + protected static class SessionReader { |
| 426 | + |
| 427 | + /** |
| 428 | + * Return changed from org.hibernate.resource.transaction.TransactionCoordinator |
| 429 | + * to org.hibernate.resource.transaction.spi.TransactionCoordinator |
| 430 | + */ |
| 431 | + protected static final Method getTransactionCoordinatorMethod; |
| 432 | + /** |
| 433 | + * Return changed from org.hibernate.resource.transaction.TransactionCoordinatorBuilder |
| 434 | + * to org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder |
| 435 | + */ |
| 436 | + protected static final Method getTransactionCoordinatorBuilderMethod; |
| 437 | + /** |
| 438 | + * Class changed from org.hibernate.resource.transaction.TransactionCoordinatorBuilder |
| 439 | + * to org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder |
| 440 | + */ |
| 441 | + protected static final Method isJtaMethod; |
| 442 | + |
| 443 | + static { |
| 444 | + try { |
| 445 | + getTransactionCoordinatorMethod = SessionImplementor.class.getMethod("getTransactionCoordinator"); |
| 446 | + } catch (Exception e) { |
| 447 | + // should never happen: the class and method exists in all versions of hibernate 5 |
| 448 | + throw new RuntimeException(e); |
| 449 | + } |
| 450 | + try{ |
| 451 | + getTransactionCoordinatorBuilderMethod = Hibernate5Version.getTransactionCoordinatorClass().getMethod("getTransactionCoordinatorBuilder"); |
| 452 | + } catch (Exception e) { |
| 453 | + // should never happen |
| 454 | + throw new RuntimeException(e); |
| 455 | + } |
| 456 | + try{ |
| 457 | + isJtaMethod = Hibernate5Version.getTransactionCoordinatorClass().getMethod("isJta"); |
| 458 | + } catch (Exception e) { |
| 459 | + // should never happen |
| 460 | + throw new RuntimeException(e); |
| 461 | + } |
| 462 | + } |
| 463 | + |
| 464 | + public static boolean isJTA(Session session) { |
| 465 | + try { |
| 466 | + Object transactionCoordinator = getTransactionCoordinatorMethod.invoke(session); |
| 467 | + Object transactionCoordinatorBuilder = getTransactionCoordinatorBuilderMethod.invoke(transactionCoordinator); |
| 468 | + return (boolean) isJtaMethod.invoke(transactionCoordinatorBuilder); |
| 469 | + } catch (Exception e) { |
| 470 | + // Should never happen |
| 471 | + throw new RuntimeException(e); |
| 472 | + } |
| 473 | + } |
| 474 | + } |
425 | 475 | }
|
0 commit comments