<div dir="ltr">I&#39;m not sure whether such a feature would pull its weight. I still think you&#39;d be best off just using a @RequestScoped bean to manage your shared state or a simple custom scope or similar facility as discussed above.<div>
<br></div><div>As you say this wouldn&#39;t work if a BV implementation parallelizes validate() calls internally, but at least the RI isn&#39;t doing this atm. and I&#39;m wondering whether it ever will. I think the problem is to find a threshold where the benefit of parallelization out-weighs its overhead which may depend on the used system, the validated model and many other things. If you have a huge model where validation in a single call takes &quot;too long&quot;, you still might consider validating sub-graphs in parallel, of course you&#39;d have to propagate your shared context yourself in this case.</div>
<div><br></div><div style>--Gunnar</div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/7/13 Thang Le <span dir="ltr">&lt;<a href="mailto:thangmle@gmail.com" target="_blank">thangmle@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Agree. Here is the issue <a href="https://hibernate.atlassian.net/browse/BVTCK-63" target="_blank">https://hibernate.atlassian.net/browse/BVTCK-63</a><div>
<br></div><div>Thanks,</div><div>Thang<div><div class="h5"><br><div class="gmail_extra"><br>
<br><div class="gmail_quote">On Thu, Jul 11, 2013 at 2:34 AM, Emmanuel Bernard <span dir="ltr">&lt;<a href="mailto:emmanuel@hibernate.org" target="_blank">emmanuel@hibernate.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Had I been in your situation, I think I would have written the<br>
uniqueness logic as a plain java code in my service layer instead of<br>
relying on Bean Validation validators. Bean Validation is very much<br>
focused around validating the value at bay (property or class).<br>
<br>
I don&#39;t think it makes sense for us to reinvent the wheel and implement<br>
some notion of scope unrelated to CDI. But it might make sense to have<br>
CDI or BV introduce a &quot;validation&quot; scope. Can you open an issue at<br>
<a href="http://beanvalidation.org/issues" target="_blank">http://beanvalidation.org/issues</a><br>
But we will likely need to find other use cases for such scope before it<br>
makes it in the spec.<br>
<span><font color="#888888"><br>
Emmanuel<br>
</font></span><div><div><br>
On Mon 2013-06-17  9:47, Thang Le wrote:<br>
&gt; Thanks, Gunnar for the suggestion.<br>
&gt;<br>
&gt; ThreadLocal object is one way to implement the scope for validators.<br>
&gt; However, as you already pointed out, using ThreadLocal introduces a<br>
&gt; different problem where developers need to clean up the data stored in<br>
&gt; local thread. It is not trivial to do it properly. Also, using ThreadLocal<br>
&gt; as a local storage admits an assumption that validation logic is executed<br>
&gt; in a single thread model. This assumption is not mentioned in the BV 1.1<br>
&gt; spec. Hence, it is likely to be broken once future BV spec defines a<br>
&gt; mechanism to allow validating process to be executed in a multi-thread<br>
&gt; model which is a next enhancement I would like to discuss with the group.<br>
&gt;<br>
&gt; ThreadLocal or my current solution to this problem is still a personal<br>
&gt; effort to solve a common problem, the scope for constraint validators, of<br>
&gt; bean validation. I would think it is better that we address this common<br>
&gt; problem in the bean validation framework level so that the feature is there<br>
&gt; when a project adopts the framework. Since constrain validators are managed<br>
&gt; by BV module, we can consider BV module is a container for all constraint<br>
&gt; validators. With that in mind, I don&#39;t see extending the framework to<br>
&gt; support scopes for constrain validators would introduce a lot of hurdles.<br>
&gt;<br>
&gt; Please let me know your thoughts.<br>
&gt;<br>
&gt; Thang<br>
&gt;<br>
&gt;<br>
&gt; On Mon, Jun 17, 2013 at 3:19 AM, Gunnar Morling &lt;<a href="mailto:gunnar@hibernate.org" target="_blank">gunnar@hibernate.org</a>&gt;wrote:<br>
&gt;<br>
&gt; &gt; 2013/6/17 Gunnar Morling &lt;<a href="mailto:gunnar@hibernate.org" target="_blank">gunnar@hibernate.org</a>&gt;<br>
&gt; &gt;<br>
&gt; &gt;&gt; If I understand right, you need some kind of scope which lasts as long as<br>
&gt; &gt;&gt; one validation call.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; You could easily implement this yourself using a ThreadLocal object,<br>
&gt; &gt;&gt; either in form of a custom CDI scope or - if you don&#39;t want to work with<br>
&gt; &gt;&gt; CDI - using a ThreadLocal object directly within your code.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; The only issue is that there is no hook/life cycle event or similar which<br>
&gt; &gt;&gt; could be used to trigger the clean up this scope once validation is over.<br>
&gt; &gt;&gt; For that purpose you could implement a proxy object for<br>
&gt; &gt;&gt; javax.validation.Validator which delegates to the actual validator<br>
&gt; &gt;&gt; implementation but handles the scope management.<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; Minor addition: If you actually decide to use CDI, I think the easiest way<br>
&gt; &gt; for doing the clean up would be via a decorator object for<br>
&gt; &gt; javax.validation.Validator [1] which delegates to the default Validator<br>
&gt; &gt; bean.<br>
&gt; &gt;<br>
&gt; &gt; --Gunnar<br>
&gt; &gt;<br>
&gt; &gt; [1]<br>
&gt; &gt; <a href="http://docs.jboss.org/weld/reference/2.0.1.Final/en-US/html_single/#decorators" target="_blank">http://docs.jboss.org/weld/reference/2.0.1.Final/en-US/html_single/#decorators</a><br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;&gt; --Gunnar<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; 2013/6/17 Thang Le &lt;<a href="mailto:thangmle@gmail.com" target="_blank">thangmle@gmail.com</a>&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;&gt; Hi all,<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt; I have done some research along the CDI support in BV 1.1. I agree that<br>
&gt; &gt;&gt;&gt; CDI as described in JSR-299 is a very powerful framework which possibly<br>
&gt; &gt;&gt;&gt; fulfills different needs for scopes of constraint validators. However, I<br>
&gt; &gt;&gt;&gt; feel reluctant to adopt such a complete CDI implementation into my project<br>
&gt; &gt;&gt;&gt; since this adoption is too much for a little gain. Beside scopes for<br>
&gt; &gt;&gt;&gt; constraint validators, all other features described in CDI are redundant<br>
&gt; &gt;&gt;&gt; for my need. Even if I use CDI implementation, I still have to write the<br>
&gt; &gt;&gt;&gt; scope for my constraint validators since there is no built-in scope that<br>
&gt; &gt;&gt;&gt; fits my need. Most of the existing scopes are tailored to use with<br>
&gt; &gt;&gt;&gt; web-beans. There is no built-in scope for object-graph validation scope<br>
&gt; &gt;&gt;&gt; which is what I need.<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt; After looking through CDI spec, I am not quite convinced that supporting<br>
&gt; &gt;&gt;&gt; scopes in Bean Validation should be done by CDI module. Since constraint<br>
&gt; &gt;&gt;&gt; validators are created by the bean validation module, would it be better<br>
&gt; &gt;&gt;&gt; that they should have different validation scopes which should also be<br>
&gt; &gt;&gt;&gt; managed by the bean validation module?<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt; Thang<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt; On Thu, Jun 13, 2013 at 9:29 AM, Thang Le &lt;<a href="mailto:thangmle@gmail.com" target="_blank">thangmle@gmail.com</a>&gt; wrote:<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt; That&#39;s great! Let me take a look into this.<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt; Thanks,<br>
&gt; &gt;&gt;&gt;&gt; Thang<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt; On Thu, Jun 13, 2013 at 2:59 AM, Gunnar Morling &lt;<a href="mailto:gunnar@hibernate.org" target="_blank">gunnar@hibernate.org</a>&gt;wrote:<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; Hi,<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; As Gerhard is saying, BV 1.1 is integrated with CDI.<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; So you could also implement your expensive operation in an application<br>
&gt; &gt;&gt;&gt;&gt;&gt; scoped bean which you @Inject into your constraint validator.<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; For BV 1.0 there is also Seam Validation [1] (not actively maintained<br>
&gt; &gt;&gt;&gt;&gt;&gt; anymore, though). When using BV with Spring, there is DI support for<br>
&gt; &gt;&gt;&gt;&gt;&gt; validators as well. So I don&#39;t think this requires any addition to the BV<br>
&gt; &gt;&gt;&gt;&gt;&gt; spec.<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; --Gunnar<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; [1] <a href="http://seamframework.org/Seam3/ValidationModule" target="_blank">http://seamframework.org/Seam3/ValidationModule</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; 2013/6/12 Gerhard Petracek &lt;<a href="mailto:gerhard.petracek@gmail.com" target="_blank">gerhard.petracek@gmail.com</a>&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; hi thang,<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; bv 1.1 allows to use cdi beans as constraint-validators.<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; (for bv 1.0 you can use add-ons like the bv module of [1] or [2].)<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; -&gt; you can use any scope you need.<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; regards,<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; gerhard<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; [1] <a href="http://myfaces.apache.org/extensions/cdi/" target="_blank">http://myfaces.apache.org/extensions/cdi/</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; [2] <a href="http://deltaspike.apache.org" target="_blank">http://deltaspike.apache.org</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; 2013/6/12 Thang Le &lt;<a href="mailto:thangmle@gmail.com" target="_blank">thangmle@gmail.com</a>&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;  Hi all,<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; I&#39;ve come across a scenario which makes me think supporting<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; different scopes for custom constraint validators would ease the task for<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; writing efficient validation logic. Similar to Spring IoC, these scopes are<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; prototype, session &amp; singleton. Currently, the Bean Validation 1.1 spec<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; suggests a &#39;prototype&#39; scope should be used for all custom constraint<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; validation classes. In my opinion, the &#39;session&#39; (likely singleton as well)<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; scope proves to be useful when a custom constraint validator needs to do<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; the SAME  timing pre-processing logic before doing some actual validation<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; logic on the current targeted object. In such cases, the developer would<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; want to store the result of the pre-processing logic into an instance<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; variable of the custom constraint validation class and reuse this result<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; later when the constraint is executed again on different objects. Below is<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; a specific use-case from my current project.<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; The model I need to validate has a tree-like structure. In this<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; model, each node can be a/an remote/access-point/cluster. I have a model<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; builder which builds the model tree given a set of nodes. The builder then<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; hands off this tree to the validator to validate it. I have some<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; constraints which require to check the uniqueness of a certain property for<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; a set of nodes (e.g: all access-points in the tree must have unique serial<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; number). These constraints are written as custom constraints. I recognize<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; these constraints follow the same pattern which is I need to traverse the<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; tree to collect all required node and build a multimap out of the collected<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; nodes and check for uniqueness using the key of the current targeted<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; object. For above example, the custom constraint is a class constraint of<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; access-point class. I need to collect all access-points in the tree, build<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; a multimap of &lt;serialNumber:access-point&gt; pairs. Then I lookup from the map<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; the collection of access-points based on the serial number of the<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; access-point being validated. It would be beneficial if the multimap of<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; &lt;serialNumber:access-point&gt; pairs can be stored in the custom validator so<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; that I don&#39;t need to re-do this expensive task again when validating other<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; access-points.<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; There might be some reasons that you wouldn&#39;t want to support<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; different scopes for custom constraint validator. However, I would think<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; when it comes to writing a constraint for a bean in relation with other<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; beans, such scopes become handy. Please let me know your thoughts on this<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; feature.<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; Thang<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; beanvalidation-dev mailing list<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; beanvalidation-dev mailing list<br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt; &gt;&gt;&gt;&gt;&gt; beanvalidation-dev mailing list<br>
&gt; &gt;&gt;&gt;&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; &gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;&gt; _______________________________________________<br>
&gt; &gt;&gt;&gt; beanvalidation-dev mailing list<br>
&gt; &gt;&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; &gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt; &gt;&gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; beanvalidation-dev mailing list<br>
&gt; &gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt; &gt;<br>
<br>
&gt; _______________________________________________<br>
&gt; beanvalidation-dev mailing list<br>
&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
<br>
_______________________________________________<br>
beanvalidation-dev mailing list<br>
<a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
</div></div></blockquote></div><br></div></div></div></div></div>
<br>_______________________________________________<br>
beanvalidation-dev mailing list<br>
<a href="mailto:beanvalidation-dev@lists.jboss.org">beanvalidation-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br></blockquote></div><br></div>