You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Robert A. Decker" <rd...@pgp.com> on 2007/09/25 02:57:18 UTC

session state vs application state?

I've read:
http://tapestry.apache.org/tapestry5/tapestry-core/guide/appstate.html

I understand that when using an ApplicationState it's really more of  
a Session state (the default way). What if I want to store something  
that's shared across all sessions as more of what I would think of as  
an ApplicationState? Do I just create a static variable somewhere?

Sorry for the basic questions I've been asking. I'm new to Tapestry  
and still trying to figure out the standard ways of doing some basic  
stuff.

R

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: session state vs application state?

Posted by Peter Stavrinides <p....@albourne.com>.
To be safe you need to use concurrent data structures, such as the 
concurrent hash map, and implement the class as a singleton.

Marcus wrote:
> Josh,
>
> Again, I agree, every method that change the _lista should be synchronized.
>
> Marcus
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: session state vs application state?

Posted by Marcus <mv...@gmail.com>.
Josh,

Again, I agree, every method that change the _lista should be synchronized.

Marcus

Re: session state vs application state?

Posted by Josh Canfield <jo...@thedailytube.com>.
On 9/25/07, Marcus <mv...@gmail.com> wrote:
>
> Hi Josh,
>
> I agree with your comment. Add synchronization to "add" method is a good
> practice.
>
> Marcus
>

Synchronizing "add" won't solve the problem. Any time you are going to
structurally change the list then any iterator becomes invalid and will
fail-fast by throwing an exception.

Check out the java5 concurrent package for some thread safe data structures
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html

-- 
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

Re: session state vs application state?

Posted by Marcus <mv...@gmail.com>.
Hi Josh,

I agree with your comment. Add synchronization to "add" method is a good
practice.

Marcus

Re: session state vs application state?

Posted by Josh Canfield <jo...@thedailytube.com>.
I'm thinking that you are going to run into problems with this technique.
While page instances are thread safe, your page class will definitely be
used by multiple threads. If you are adding to _lista while iterating over
it then you are going to get an ConcurrentModificationException. Performing
actions on this list are going to require considering synchronization
issues.

Of course, this consideration is going to be required of any object that you
store at the application level (in the servlet context for instance) if you
are doing anything other than read only operations.

Josh


On 9/25/07, Marcus <mv...@gmail.com> wrote:
>
> Hi Robert,
>
> Anyway, if you create a class with static methods and attributes, your
> object will be shared for entire application.
>
> public class ListaEstatica {
>
>        private static List<Usuario> _lista;
>
>        public static List<Usuario> getLista() {
>                if (_lista==null)
>                        _lista=new ArrayList<Usuario>(0);
>                return _lista;
>        }
>
>        public static void add(Usuario usuario) { getLista().add(usuario);
> }
> }
>
>
> Marcus
>



-- 
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

Re: session state vs application state?

Posted by Marcus <mv...@gmail.com>.
Hi Robert,

Anyway, if you create a class with static methods and attributes, your
object will be shared for entire application.

public class ListaEstatica {

	private static List<Usuario> _lista;
	
	public static List<Usuario> getLista() {
		if (_lista==null)
			_lista=new ArrayList<Usuario>(0);
		return _lista;
	}

	public static void add(Usuario usuario) { getLista().add(usuario); }
}		


Marcus

Re: session state vs application state?

Posted by Howard Lewis Ship <hl...@gmail.com>.
You really want  your object to be stored in the ServletContext.  In
Tapestry terms, that would be possible as a new (as in, not yet provided by
the framework) ApplicationStatePersistenceStrategy.  Feel free to add a JIRA
Issue (and a patch!).

On 9/24/07, Robert A. Decker <rd...@pgp.com> wrote:
>
> I've read:
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/appstate.html
>
> I understand that when using an ApplicationState it's really more of
> a Session state (the default way). What if I want to store something
> that's shared across all sessions as more of what I would think of as
> an ApplicationState? Do I just create a static variable somewhere?
>
> Sorry for the basic questions I've been asking. I'm new to Tapestry
> and still trying to figure out the standard ways of doing some basic
> stuff.
>
> R
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
Partner and Senior Architect at Feature50

Creator Apache Tapestry and Apache HiveMind