Set DB Adapter in code

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Set DB Adapter in code

Hugi Thordarson
Hi all,
when running tests I need to switch the DB adapter to H2Adapter (my DataNode has a Customer adaptor). How would this be easiest to do?

Cheers,
- hugi
Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

Michael Gentry-2
Here is one example of using SimpleJNDI to bind to a local H2 DB for
testing (assuming your normal application uses JNDI):

https://github.com/mrg/cbe/blob/master/UnitTesting/src/test/java/cbe/testing/AppTest.java

The example is still on Cayenne 3.0.2, but hopefully that'll help.

mrg


On Tue, Oct 18, 2016 at 12:26 PM, Hugi Thordarson <[hidden email]> wrote:

> Hi all,
> when running tests I need to switch the DB adapter to H2Adapter (my
> DataNode has a Customer adaptor). How would this be easiest to do?
>
> Cheers,
> - hugi
Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

John Huss
In reply to this post by Hugi Thordarson
Something like this?

Collection<DataNode> nodes = runtime.getDataDomain().getDataNodes();

for (DataNode node : nodes) {

node.setAdapter(myNewAdapter);

}

On Tue, Oct 18, 2016 at 11:27 AM Hugi Thordarson <[hidden email]> wrote:

> Hi all,
> when running tests I need to switch the DB adapter to H2Adapter (my
> DataNode has a Customer adaptor). How would this be easiest to do?
>
> Cheers,
> - hugi
Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

Hugi Thordarson
In reply to this post by Michael Gentry-2
Yeah, I’m managing to connect to the DB fine, but it’s still using the Custom Adapter set in my data node, I want to override that.

Cheers,
- hugi




> On 18. okt. 2016, at 16:34, Michael Gentry <[hidden email]> wrote:
>
> Here is one example of using SimpleJNDI to bind to a local H2 DB for
> testing (assuming your normal application uses JNDI):
>
> https://github.com/mrg/cbe/blob/master/UnitTesting/src/test/java/cbe/testing/AppTest.java
>
> The example is still on Cayenne 3.0.2, but hopefully that'll help.
>
> mrg
>
>
> On Tue, Oct 18, 2016 at 12:26 PM, Hugi Thordarson <[hidden email]> wrote:
>
>> Hi all,
>> when running tests I need to switch the DB adapter to H2Adapter (my
>> DataNode has a Customer adaptor). How would this be easiest to do?
>>
>> Cheers,
>> - hugi

Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

Hugi Thordarson
In reply to this post by John Huss
Thanks John, I had made it just about that far, but how do I obtain an instance of an adapter?

Cheers,
- hugi


> On 18. okt. 2016, at 16:56, John Huss <[hidden email]> wrote:
>
> Something like this?
>
> Collection<DataNode> nodes = runtime.getDataDomain().getDataNodes();
>
> for (DataNode node : nodes) {
>
> node.setAdapter(myNewAdapter);
>
> }
>
> On Tue, Oct 18, 2016 at 11:27 AM Hugi Thordarson <[hidden email]> wrote:
>
>> Hi all,
>> when running tests I need to switch the DB adapter to H2Adapter (my
>> DataNode has a Customer adaptor). How would this be easiest to do?
>>
>> Cheers,
>> - hugi

Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

John Huss
Hmmm, not sure.  Another option is to define both nodes in CayenneModeler
and remove the one that you don't need at runtime before running any
queries or commits.

On Tue, Oct 18, 2016 at 12:19 PM Hugi Thordarson <[hidden email]> wrote:

> Thanks John, I had made it just about that far, but how do I obtain an
> instance of an adapter?
>
> Cheers,
> - hugi
>
>
> > On 18. okt. 2016, at 16:56, John Huss <[hidden email]> wrote:
> >
> > Something like this?
> >
> > Collection<DataNode> nodes = runtime.getDataDomain().getDataNodes();
> >
> > for (DataNode node : nodes) {
> >
> > node.setAdapter(myNewAdapter);
> >
> > }
> >
> > On Tue, Oct 18, 2016 at 11:27 AM Hugi Thordarson <[hidden email]>
> wrote:
> >
> >> Hi all,
> >> when running tests I need to switch the DB adapter to H2Adapter (my
> >> DataNode has a Customer adaptor). How would this be easiest to do?
> >>
> >> Cheers,
> >> - hugi
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

Mike Kienenberger
The way I set up for testing to use a different config xml file
pointing to my in-memory hsqldb nodes and pass it to the ServerRuntime
constructor.

On Tue, Oct 18, 2016 at 1:44 PM, John Huss <[hidden email]> wrote:

> Hmmm, not sure.  Another option is to define both nodes in CayenneModeler
> and remove the one that you don't need at runtime before running any
> queries or commits.
>
> On Tue, Oct 18, 2016 at 12:19 PM Hugi Thordarson <[hidden email]> wrote:
>
>> Thanks John, I had made it just about that far, but how do I obtain an
>> instance of an adapter?
>>
>> Cheers,
>> - hugi
>>
>>
>> > On 18. okt. 2016, at 16:56, John Huss <[hidden email]> wrote:
>> >
>> > Something like this?
>> >
>> > Collection<DataNode> nodes = runtime.getDataDomain().getDataNodes();
>> >
>> > for (DataNode node : nodes) {
>> >
>> > node.setAdapter(myNewAdapter);
>> >
>> > }
>> >
>> > On Tue, Oct 18, 2016 at 11:27 AM Hugi Thordarson <[hidden email]>
>> wrote:
>> >
>> >> Hi all,
>> >> when running tests I need to switch the DB adapter to H2Adapter (my
>> >> DataNode has a Customer adaptor). How would this be easiest to do?
>> >>
>> >> Cheers,
>> >> - hugi
>>
>>
Reply | Threaded
Open this post in threaded view
|

Re: Set DB Adapter in code

Andrus Adamchik
In reply to this post by Hugi Thordarson

> On Oct 18, 2016, at 7:26 PM, Hugi Thordarson <[hidden email]> wrote:
>
> Hi all,
> when running tests I need to switch the DB adapter to H2Adapter (my DataNode has a Customer adaptor). How would this be easiest to do?
>
> Cheers,
> - hugi


I am a bit late to the game here, but a general customization advice (with a few exceptions) is to go to ServerModule and see what you can redefine there. In this case DbAdapterFactory is the best candidate.

Also an alternative solution. Instead of defining your custom adapter explicitly in the Model, let your app guess it automatically based on what DB you connect:

  binder.bindList(Constants.SERVER_ADAPTER_DETECTORS_LIST).add(MyAdapterDetector.class).before(FirebirdSniffer.class);

"MyAdapterDetector" is a class that implements DbAdapterDetector. Look for existing implementations for an example. It should return a non-null value when it detects that DB signature matches your real DB.

"FirebirdSniffer" is just a marker .. Our standard detector list starts with it. So your custom detector will be the first one consulted by Cayenne, as it will go *before* FirebirdSniffer.

So when the DB is your real DB, your own adapter will be installed. When the DB is H2, Cayenne will skip MyAdapterDetector, and will eventually find detector for H2 and install the corresponding adapter.

Andrus