Skip to content

IllegalStateException on Transaction Scoped Beans #734

Closed
@seandcrouse

Description

@seandcrouse

if (collection.size() > 0 && collection.iterator().next() instanceof Declarable) {

Beans using org.springframework.transaction.support.SimpleTransactionScope fail to get instantiated because RabbitAdmin is not operating inside a transaction. I have a message producer that filters out duplicate messages per transaction. This is done with a transaction scoped Set bean. I know that my producer will only use this bean within that transaction. However, RabbitAdmin is getting every Collection bean, calling the size() method, which causes the class scoped proxy to try to instantiate the bean. This fails because it's not happening inside a transaction.

Sample code:

    public static String TRANSACTION_SCOPE = "transaction";
    
    @Bean(name = "sentBundles")
    @Scope(value = TRANSACTION_SCOPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Set<MessageBundle> sentBundles() {
        return new HashSet<>();
    }

    @Bean
    public static CustomScopeConfigurer customScope() {
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();
        configurer.addScope(TRANSACTION_SCOPE, new SimpleTransactionScope());
        return configurer;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions