Commit and Optimize are operations to make updates available to searchers. They are intended to be run after updates, not before queries.
Furthermore, they are expensive operations, which is why you're getting sporadical timeouts. Unless you have some special requirements, I recommend setting the <autoCommit/>
option in your solrconfig.xml. As the name says, it will automatically issue the commit depending on configurable criteria like maximum number of uncommitted documents or maximum time after adding documents.
Optimize is even more expensive than Commit, it basically rewrites the index. The frequency of an Optimize depends on how often you Commit changes and how many changes there are per commit.
See also: