Those are probably contributing factors, I imagine most mass shooters feel stuck and squeezed and angry about it to the point of violence against (usually) unrelated parties. Community, opportunity, just feeling like one's society gives a shit about them, would all make a big difference. Unfortunately when this desire gets filled with something irrational and toxic like religion, it can lead to other problems long-term.
Making sure the average person has enough economic freedom to not feel stuck or exploited would go a long way to preventing this, so would meaningful public mental health resources and gun control.