Error in custom provider while locating Context - Roboguice 2.0 + Ormlite

366 views
Skip to first unread message

Marco Serioli

unread,
Oct 8, 2012, 9:10:01 AM10/8/12
to robo...@googlegroups.com
Hi everyone!

I'm trying to Inject my TransactionManagerProvider..

public class TransactionManagerProvider implements Provider<TransactionManager> {

@Inject
private DatabaseProvider databaseProvider;

@Override
public TransactionManager get() {
ConnectionSource connectionSource = databaseProvider.get().getConnectionSource();
return new TransactionManager(connectionSource);
}
}

where DatabaseProvider:

public class DatabaseProvider implements Provider<OrmLiteSqliteOpenHelper> {
@Inject
private Provider<Context> contextProvider;
@Override
public OrmLiteSqliteOpenHelper get() {
return OpenHelperManager.getHelper(contextProvider.get(), MyDatabaseOpenHelper.class);
}
}

But when I call it:

transactionManagerProvider.get().callInTransaction(new Callable<Void>() {
//call to daos methods and logic
}
I got an exception: 

1) Error in custom provider, java.util.EmptyStackException
 at roboguice.config.DefaultRoboModule.configure(DefaultRoboModule.java:130)
   while locating android.content.Context
 1 error
  at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
  at it.cpmapave.mt.service.InterventoServiceImpl.synkInterventi(InterventoServiceImpl.java:109)
  at it.cpmapave.mt.aservice.SynkService.onHandleIntent(SynkService.java:98)
  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:132)
  at android.os.HandlerThread.run(HandlerThread.java:60)
 Caused by: java.util.EmptyStackException
  at java.util.Stack.peek(Stack.java:58)
  at roboguice.inject.ContextScope$1.get(ContextScope.java:108)
  at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
  at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
  at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
  at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
  ... 6 more

But if I comment out the line that create the transaction and I run only the dao methods inside it works!

The problem as the exception states is that roboguice cannot access the Context.. But I don't know why the code inside transaction works.. the context is the same!

Maybe that code inside transaction works because the daos are created in an Activity context? and that the transaction don't work because the TransactionManager is created inside a Service context?

Thank you for your response..
Marco



Marco Serioli

unread,
Oct 9, 2012, 2:27:31 AM10/9/12
to robo...@googlegroups.com
I have found that I need to use a ContextScopeProvider.

So I have changed

@Inject private Provider<TransactionManager> transactionManagerProvider;

into 

@Inject private ContextScopedProvider<TransactionManager> transactionManagerProvider;

But if the call is made both from an Activity and a Service.. I must use ContextScopedProvider and than pass always the context to the get method or there's a better way?

Thank you for any reposnse!
Marco
Reply all
Reply to author
Forward
0 new messages