You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Oilid Adsi <Oi...@freenet-ag.de> on 2007/11/28 09:59:33 UTC

Cayenneand Thread-Bound DataContext

Hello,

I tried to use a Thread-Bound DataContext with Cayenne 2.0.3
(http://cwiki.apache.org/CAYDOC/obtaining-datacontext.html):

[code]
public DataContext getDataContext() {
	try {
		logger.debug("trying to get threaded-data-context for
thread " + Thread.currentThread());
		return DataContext.getThreadDataContext();
	} catch (IllegalStateException e) {
		logger.debug("no threaded data-context, creating one");
		DataContext context = DataContext.createDataContext();
		logger.debug("binding new dataContext to current-thread
" + Thread.currentThread());
		DataContext.bindThreadDataContext(context);
		return context;
	}
}
[/code]

But with this construct I noticed the following behaviour:

If I'm sending a "wrong" INSERT-statement by the web-frontend the first
time I will get a right error/exception (MysqlDataTruncation).
Afterwards I send again a second request with a right dataset to be
inserted in the DB which would not fail normally.
But because of the threaded dataContext the old/first task (which is
wrong) is still binded in the second request.

Here is the part of the log for the second request:

[log]
2007-10-15 13:06:14,921 [http-8080-2] [INFO ] [] QueryLogger - INSERT
INTO insured_event (accessory, all_data_complete, article_number,
collection_date, confirmation_of_receipt, contract_id,
contribution_customer_amount, contribution_tariff_amount,
cover_note_date, created, customer_id, damage_category_id, damage_date,
device_provider, device_type, event_state_id, imei, incoming_date,
insured_event_closed_date, insured_event_type_id, police_reference,
police_report, repair_cost_amount, subst_device_provider,
subst_device_type, updated, valuation_price_amount, workflow_reference)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?)

2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - [bind:
'', 'false', NULL, NULL, 'false',
'AEkARAAjAE0AQwBbADEAMgAzADAAOAAyADgAXQAA..', NULL, 326.0, NULL,
'2007-10-15 13:06:04.578', 'AEkARAAjAE0AQwBbADMAMgAyADcAMwA1ADYAXQAA..',
3, NULL, NULL, NULL, 4, '351453207158847', '2007-10-16 00:00:00.0',
NULL, 2, '', 'false', NULL, NULL, NULL, '2007-10-15 13:06:04.578', NULL,
NULL]

2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - ===
updated 1 row.

2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - [bind:
'', 'false', NULL, NULL, 'false',
'AEkARAAjAE0AQwBbADEAMgAzADAAOAAyADgAXQAA..', NULL, 5000.0, NULL,
'2007-10-15 13:05:44.734', 'AEkARAAjAE0AQwBbADMAMgAyADcAMwA1ADYAXQAA..',
3, NULL, NULL, NULL, 4, '351453207158847', '2007-10-16 00:00:00.0',
NULL, 2, '', 'false', NULL, NULL, NULL, '2007-10-15 13:05:44.734', NULL,
NULL]

2007-10-15 13:06:14,953 [http-8080-2] [INFO ] [] QueryLogger - ***
error.

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated; out
of range for column 'contribution_tariff_amount' at row 1
[/log]

The value "326.0" is right and "5000.0" is the wrong one.

Is this a bug or an excepted behaviour?
Is this reproducible for you?

BTW: The servlet filter in the web.xml is set in this scenario
(http://cayenne.apache.org/doc20/web-applications.html).

Thanks for help!

Oilid

Re: Cayenneand Thread-Bound DataContext

Posted by Michael Gentry <bl...@gmail.com>.
Hi Oilid,

If I understand you correctly (and there is a chance I do not), you
are doing these steps in a web application:

1) Session is created
2) Enter data into a form with a value of 5000.0 for contribution_tariff_amount
3) Create new Cayenne object (added to DataContext) with 5000.0 for
contribution_tariff_amount and get an exception when attempting to
commitChanges() to DB and show error to user with the same edit page
4) Fix error and enter 326.0 and re-submit
5) Create new Cayenne object (added to DataContext) with 326.0 for
contribution_tariff_amount and get an exception when attempting to
commitChanges() to DB because of the previous 5000.0 value

If that is the scenario, then yes, it is the correct behavior.  You
have two objects in your DataContext (one with 5000.0 and one with
326.0) and Cayenne attempts to insert both of them.  When the first
commitChanges() failed, the object remained in the DataContext in an
un-saved state, so Cayenne will attempt to insert it again on the next
commitChanges().

I'm not sure which web framework you are using, but I find it easier
to only create one Cayenne object for them to edit.  If using
something like Tapestry, it is very easy to bind the Cayenne object
data fields to the Tapestry/HTML form items so you are directly
editing the Cayenne object.

Hope that helps!

/dev/mrg


On Nov 28, 2007 3:59 AM, Oilid Adsi <Oi...@freenet-ag.de> wrote:
> Hello,
>
> I tried to use a Thread-Bound DataContext with Cayenne 2.0.3
> (http://cwiki.apache.org/CAYDOC/obtaining-datacontext.html):
>
> [code]
> public DataContext getDataContext() {
>         try {
>                 logger.debug("trying to get threaded-data-context for
> thread " + Thread.currentThread());
>                 return DataContext.getThreadDataContext();
>         } catch (IllegalStateException e) {
>                 logger.debug("no threaded data-context, creating one");
>                 DataContext context = DataContext.createDataContext();
>                 logger.debug("binding new dataContext to current-thread
> " + Thread.currentThread());
>                 DataContext.bindThreadDataContext(context);
>                 return context;
>         }
> }
> [/code]
>
> But with this construct I noticed the following behaviour:
>
> If I'm sending a "wrong" INSERT-statement by the web-frontend the first
> time I will get a right error/exception (MysqlDataTruncation).
> Afterwards I send again a second request with a right dataset to be
> inserted in the DB which would not fail normally.
> But because of the threaded dataContext the old/first task (which is
> wrong) is still binded in the second request.
>
> Here is the part of the log for the second request:
>
> [log]
> 2007-10-15 13:06:14,921 [http-8080-2] [INFO ] [] QueryLogger - INSERT
> INTO insured_event (accessory, all_data_complete, article_number,
> collection_date, confirmation_of_receipt, contract_id,
> contribution_customer_amount, contribution_tariff_amount,
> cover_note_date, created, customer_id, damage_category_id, damage_date,
> device_provider, device_type, event_state_id, imei, incoming_date,
> insured_event_closed_date, insured_event_type_id, police_reference,
> police_report, repair_cost_amount, subst_device_provider,
> subst_device_type, updated, valuation_price_amount, workflow_reference)
> VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
> ?, ?, ?, ?, ?, ?, ?)
>
> 2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - [bind:
> '', 'false', NULL, NULL, 'false',
> 'AEkARAAjAE0AQwBbADEAMgAzADAAOAAyADgAXQAA..', NULL, 326.0, NULL,
> '2007-10-15 13:06:04.578', 'AEkARAAjAE0AQwBbADMAMgAyADcAMwA1ADYAXQAA..',
> 3, NULL, NULL, NULL, 4, '351453207158847', '2007-10-16 00:00:00.0',
> NULL, 2, '', 'false', NULL, NULL, NULL, '2007-10-15 13:06:04.578', NULL,
> NULL]
>
> 2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - ===
> updated 1 row.
>
> 2007-10-15 13:06:14,937 [http-8080-2] [INFO ] [] QueryLogger - [bind:
> '', 'false', NULL, NULL, 'false',
> 'AEkARAAjAE0AQwBbADEAMgAzADAAOAAyADgAXQAA..', NULL, 5000.0, NULL,
> '2007-10-15 13:05:44.734', 'AEkARAAjAE0AQwBbADMAMgAyADcAMwA1ADYAXQAA..',
> 3, NULL, NULL, NULL, 4, '351453207158847', '2007-10-16 00:00:00.0',
> NULL, 2, '', 'false', NULL, NULL, NULL, '2007-10-15 13:05:44.734', NULL,
> NULL]
>
> 2007-10-15 13:06:14,953 [http-8080-2] [INFO ] [] QueryLogger - ***
> error.
>
> com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated; out
> of range for column 'contribution_tariff_amount' at row 1
> [/log]
>
> The value "326.0" is right and "5000.0" is the wrong one.
>
> Is this a bug or an excepted behaviour?
> Is this reproducible for you?
>
> BTW: The servlet filter in the web.xml is set in this scenario
> (http://cayenne.apache.org/doc20/web-applications.html).
>
> Thanks for help!
>
> Oilid
>