9

I have searched StackOverflow and the web for an answer to this question, but I could not find an answer. When I run my application on Gingerbread, it runs fine. But when I run it on 4.2.2, I get this error: java.lang.IllegalStateException Cannot perform this operation because the connection pool has been closed I have a SherlockFragmentActivity which contains two fragments.

Fragment 1:

public final class TodoFragment extends SherlockListFragment {
NotesDbAdapter db;

private static final int DELETE_ID = Menu.FIRST + 1;

public static TodoFragment newInstance(String s) {
    TodoFragment fragment = new TodoFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new NotesDbAdapter(getActivity());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    return ll;

}

Fragment 2:

public final class NotesFragment extends SherlockListFragment {
NotesDbAdapter db;
private static final int ACTIVITY_EDIT = 1;

private static final int DELETE_ID = Menu.FIRST + 1;

public static NotesFragment newInstance(String content) {
    NotesFragment fragment = new NotesFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new NotesDbAdapter(getActivity());

    db.open();
    if (db.fetchAllNotes().getCount() == 0) {
        doWelcomeMessage();
    }
    db.close();

}

private void doWelcomeMessage() {
    // Show welcome dialog
    startActivity(new Intent(getActivity(), NotesWelcome.class));

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    db = new NotesDbAdapter(getActivity());

    db.open();
    if (db.fetchAllNotes().getCount() == 0) {
        doWelcomeMessage();
    }
    db.close();


    return ll;

}

SherlockFragmentActivity:

public class NotesFragmentActivity extends SherlockFragmentActivity {

ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(bar.newTab().setText("Notes"), NotesFragment.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText("Todo List"),
            TodoFragment.class, null);

}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
}

I've been stuck on this for HOURS (no jokes), and would really appreciate any help. Thanks again.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
  • Post whole stacktrace. This error means application closes db somewhere and another db connection occurs after that but db is closed – kinghomer Oct 08 '13 at 11:01
  • you have to remove close operation . try by removing close() . this error is due to closing the db. – Zar E Ahmer Aug 19 '14 at 08:33

2 Answers2

9

SQLiteConnectionPooling was added in after Android 4.1. Whereas in previous versions a cursor object would simply try to reopen a database if a query or other operation was made on a connection that was closed, with Connection Pooling, an exception is thrown. The NotesDBAdapter is probably a throwback which may have broken compatibility with 4.2.2.

user2312380
  • 126
  • 1
  • 2
    So, how do we solve that? I'm getting this error sometimes in my app. When in some parts of the code I call cursor.moveToFirst(), then I receive the error: Cannot perform this operation because the connection pool has been closed. – Ferran Negre Apr 25 '14 at 12:35
  • I wrote this topic http://stackoverflow.com/questions/23293572/android-cannot-perform-this-operation-because-the-connection-pool-has-been-clos/23293930?noredirect=1#23293930 . Read the answer and my comment on that answer :) – Ferran Negre May 26 '14 at 13:23
2

I got the same issue, because multiple threads accessed the same SQLiteOpenHelperand therefore opened and closed the connection simultaneously.

I solved it by adding a userCount attribute to the helper. Now the helper only closes the connection, if the userCount is zero, i.e. no other threads are currently using the database.

Kilian Obermeier
  • 6,678
  • 4
  • 38
  • 50