Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: isUnwrappableAs and unwrap in ConnectionProvider
PostPosted: Mon Aug 14, 2017 7:18 am 
Newbie

Joined: Mon Aug 14, 2017 6:33 am
Posts: 2
Hi all,
We are implementing a custom ConnectionProvider implementation for supporting multidatabase multitenancy (we are using hibernate 4.2.x in OSGi environment).
Our custom ConnectionProvider is mysql datasource based.
We are wondering what is the best implementation for the methods isUnwrappableAs and unwrap. We are looking at some of the OOB implementations included in Hibernate distribution but we are quite confused:

- The single tenant datasource-based connection provider like org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl allows the unwrapping of the DataSource.

- However a multitenant datasource-based implementation like org.hibernate.service.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl only allows the unwrapping of MultiTenantConnectionProvider and AbstractMultiTenantConnectionProvider.

The javadoc of org.hibernate.service.spi.Wrapped interface (the one defining the methods isUnwrappableAs and unwrap) states: "Optional contract for services that wrap stuff that to which it is useful to have access. For example, a service that maintains a javax.sql.DataSource might want to expose access to the javax.sql.DataSource or its java.sql.Connection instances"

Could our hibernate gurus explain us what are the best implementation for these two methods?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: isUnwrappableAs and unwrap in ConnectionProvider
PostPosted: Mon Aug 14, 2017 7:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1432
Quote:
- The single tenant datasource-based connection provider like org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl allows the unwrapping of the DataSource.


That's because the DatasourceConnectionProviderImpl wraps a single DataSource so we know to which dataSource to unwrap.

Quote:
- However a multitenant datasource-based implementation like org.hibernate.service.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl only allows the unwrapping of MultiTenantConnectionProvider and AbstractMultiTenantConnectionProvider.


For a multitenant environment where you have multiple DataSources, which one do you think we could choose if we decide to unwrap to a DataSource? remamber that this method does not take a tenant identifier.

Quote:
Could our hibernate gurus explain us what are the best implementation for these two methods?


I tried to find where these two methods are used, and couldn't find any specific place where they are needed. Do you have any specific issue related to them? Otherwise, just use the AbstractMultiTenantConnectionProvider logic.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: isUnwrappableAs and unwrap in ConnectionProvider
PostPosted: Mon Aug 14, 2017 9:28 am 
Newbie

Joined: Mon Aug 14, 2017 6:33 am
Posts: 2
Thanks for the very quick response!

Quote:
For a multitenant environment where you have multiple DataSources, which one do you think we could choose if we decide to unwrap to a DataSource? remamber that this method does not take a tenant identifier.


We could use a threadlocal variable in order to track the current tenant. Before unwrapping the datasource we could get the current tenant value from the threadlocal var. Could it make sense?

Quote:
Do you have any specific issue related to them?


Not at the moment :-), we are just wondering (like you) if there are any external frameworks or other hibernate's pieces of code that need these methods. The only piece of code we found is class org.springframework.orm.hibernate3.SessionFactoryUtils that is deprecated.

Our question is whether Wrapped methods are really used anywhere :-)


Top
 Profile  
 
 Post subject: Re: isUnwrappableAs and unwrap in ConnectionProvider
PostPosted: Mon Aug 14, 2017 10:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1432
Quote:
We could use a threadlocal variable in order to track the current tenant. Before unwrapping the datasource we could get the current tenant value from the threadlocal var. Could it make sense?


That could work, indeed.

Quote:
Our question is whether Wrapped methods are really used anywhere :-)


I'm not aware of any particular framework or use case that depends on these two methods. One option is to just throw UnsupportedOperationException and think about the best way to implement them once you have an actual use case from a client and have more context on the problem.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.