I probably didn't explain myself very well,  I came up with a solution that 
seems to work.  I created the following class

public class ObjectODB {

    static final ThreadLocal<OObjectDatabaseTx> localObjectTx = new 
ThreadLocal<OObjectDatabaseTx>();

    public static void set(OObjectDatabaseTx user) {
        localObjectTx.set(user);
    }

    public static void unset() {
        localObjectTx.remove();
    }

    public static OObjectDatabaseTx get() {
        return localObjectTx.get();
    }

}

and in my servlet filter


public class ObjectDbFilterThreadLocal implements Filter {

    private WebApplicationContext springContext;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        springContext = WebApplicationContextUtils
                .getWebApplicationContext(filterConfig.getServletContext());
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        try {
            Credentials creds = (Credentials) springContext.getBean("creds"
);

            OObjectDatabaseTx db = OObjectDatabasePool.global().acquire(
                    creds.getiURL(), creds.getiUserName(),
                    creds.getiUserPassword());

            db.getEntityManager().registerEntityClasses("some.packages");

            ODatabaseRecordThreadLocal.INSTANCE.set(db.getUnderlying());
            ObjectODB.set(db);

            try {
                chain.doFilter(request, response);

            } finally {
                ObjectODB.unset();
                db.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
            ((HttpServletResponse) response)
                    .setStatus(HttpServletResponse.SC_FORBIDDEN);
        }
    }

    @Override
    public void destroy() {

    }
}



I can now call 

db = ObjectODB.get();



in my DAO classes,


On Wednesday, 29 January 2014 16:54:09 UTC, Andrey Lomakin wrote:
>
> Hi Francis,
> When open database (acquire from pool) property 
> ODatabaseRecordThreadLocal.INSTANCE is automatically set.
> Acquiring connection from poo is cheap operation, so you can do something 
> like:
> OObjectDatabaseTx db = OObjectDatabasePool.global().acquire(dbURL,dbUser, 
> dbPwd);
> try {
> } finally {
> db.close();
> }
>
> In your methods without performance loss.
>
>
> On Mon, Jan 27, 2014 at 12:00 AM, Francis West <[email protected]<javascript:>
> > wrote:
>
>> Hello I am trying to use OObjectDatabaseTx with a webapp, i thought i 
>> might follow the example in the documentation but just use the 
>> OObjectDatabasePool to get a OObjectDatabaseTx,
>>
>> so something like:
>>
>> OObjectDatabaseTx db = OObjectDatabasePool.global().acquire(dbURL,dbUser, 
>> dbPwd);
>> ODatabaseRecordThreadLocal.INSTANCE.set(db.getUnderlying());
>>
>> it seems I can't put the OObjectDatabaseTx db object in the ThreadLocal 
>> so i have to use the getUnderlying() method,
>>
>> where I am stuck is creating myself an OObjectDatabaseTx outside of my 
>> servlet filter:
>>
>>
>> ODatabaseDocument localdbthread = (ODatabaseDocument) 
>> ODatabaseRecordThreadLocal.INSTANCE.get();
>>
>> i assume i need to do something more to turn localdbthread back into a 
>> OObjectDatabaseTx  or create an instance of OObjectDatabaseTx using 
>> ODatabaseDocument?
>>
>> Thanks in advance
>> Francis
>>
>>
>>  -- 
>>  
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "OrientDB" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> -- 
> Best regards,
> Andrey Lomakin.
>
> Orient Technologies
> the Company behind OrientDB
>
>  

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to