I see what you mean with the transaction. If I inject the
EntityManager into a LocalClient without the EJB, it throws a
TransactionRequiredException. Hmm.

Let me see what can be done for this.

Quintin Beukes



On Sat, Oct 3, 2009 at 7:31 PM, Laird Nelson <ljnel...@gmail.com> wrote:
> Sure:
>
> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
>
> And:
>
>    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
> LocalInitialContextFactory.class.getName());
>    // See
> http://blog.jonasbandi.net/2009/06/restarting-embedded-openejb-container.html
> :
>    properties.setProperty("openejb.embedded.initialcontext.close",
> "destroy");
>
> To create the database:
>
> The database name is: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 (close delay -1
> means that the db won't shut down until the VM does.  If you set it to 0, it
> shuts down the moment there are no more connections to it.  My problem is I
> don't know how to influence OpenEJB's underlying connection pool such that
> (a) I can guarantee exactly one connection will be open to H2 while the
> LocalInitialContext is up and (b) I can guarantee that when the context is
> closed, so is the connection pool.)
>
> So the properties look like this:
>
>      final String name = "test";
>      properties.setProperty(name, "new://Resource?type=DataSource"); //
> create a data source named "test"
>      properties.setProperty(name + ".JdbcUrl",
> "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
>      properties.setProperty(name + ".JdbcDriver", "org.h2.Driver");
>      properties.setProperty(name + ".Username", "sa");
>      properties.setProperty(name + ".Password", "");
>
> Hope that helps.
>
> Best,
> Laird
>
> On Sat, Oct 3, 2009 at 1:20 PM, Quintin Beukes <quin...@skywalk.co.za>wrote:
>
>> Basically, all I want to know is your PersistenceProvider class, your
>> data resource InitialContext configuration for the test, and how you
>> create the database in H2.
>>
>> I got the H2 server running, but went to go shower, so haven't done
>> anything beyond that.
>>
>> Quintin Beukes
>>
>>
>>
>> On Sat, Oct 3, 2009 at 6:55 PM, Laird Nelson <ljnel...@gmail.com> wrote:
>> > I'll try to put it together; it's spread out over a large project base at
>> > the moment.
>> >
>> > Cheers,
>> > Laird
>> >
>> > On Sat, Oct 3, 2009 at 12:47 PM, Quintin Beukes <quin...@skywalk.co.za
>> >wrote:
>> >
>> >> Hey,
>> >>
>> >> This sounds interesting. Actually a nice way to do tests from a clean
>> >> db, a problem I have myself which I circumvented with a modified
>> >> Hibernate.
>> >>
>> >> Can you send me your H2 OpenEJB configuration. I want to set it up and
>> >> see if I can get it going. Will let you know what I come up with -
>> >> even if it means to make a simple junit runner just for this purpose.
>> >> It would be great to have tests run on a clean db everytime.
>> >>
>> >> Quintin Beukes
>> >>
>> >>
>> >>
>> >> On Sat, Oct 3, 2009 at 6:42 PM, Laird Nelson <ljnel...@gmail.com>
>> wrote:
>> >> > On Sat, Oct 3, 2009 at 12:07 PM, Quintin Beukes <
>> quin...@skywalk.co.za
>> >> >wrote:
>> >> >
>> >> >> Yes,it is managed by the container and the context. The initial
>> >> >> context will close with the VM. Afaik it will have the same behaviour
>> >> >> after the test closes as the EJB would.
>> >> >
>> >> >
>> >> > OK; good to know.
>> >> >
>> >> > From you e-mail I figure your focus is on the automatic destruction of
>> >> >>
>> >> > the db.
>> >> >
>> >> >
>> >> > Yes.  H2, specifically.
>> >> >
>> >> > I've *never* had luck with the DB being automatically destroyed
>> between
>> >> > tests.  I've closed the context, used the magic property
>> (destroyOnClose?
>> >> > don't remember offhand), tried setting up H2 without the
>> DB_CLOSE_DELAY
>> >> > setting, *with* the DB_CLOSE_DELAY setting--nothing.  It might have to
>> do
>> >> > with the strange intersection of (a) there being a connection pool and
>> >> (b)
>> >> > H2's being an in-memory database that tries to destroy itself when it
>> >> thinks
>> >> > it's safe to do so.
>> >> >
>> >> > I can verify this because when I have two test methods that each try
>> to
>> >> > persist an object with the same ID--which would succeed in a blank
>> >> > database--my second method fails.
>> >> >
>> >> > To be clear, then, I want my named H2 database to be wiped out and
>> >> > recreated--complete with DDL generation as described by my JPA
>> >> > mappings--before each test method is run.
>> >> >
>> >> > To put it one final way, I want my test code to look like this:
>> >> > X x = new X();
>> >> > this.em.persist(x);
>> >> >
>> >> > ...i.e. no beginning transactions, no shutting down databases, no
>> mucking
>> >> > about with EntityManagerFactories, etc. etc. etc.  Just bare bones
>> >> > simplicity.  Frankly OpenEJB need not even be involved, but it seemed
>> >> like
>> >> > an easy black box that would enable most of these kinds of tests.
>> >> >
>> >> > Thanks for getting involved,
>> >> > Laird
>> >> >
>> >>
>> >
>>
>

Reply via email to