@Bew Welcome to the Forum!
A couple of points:
1) Your post imples that your first chain lasted three years before breaking. How many miles did the chain have on it? Many on here complain when their chain wears out at less than 600 miles. You have either been lucky that your chain lasted, or maybe you do very few miles. Even if your bike is lightly used, chains suffer from corrosion just from the passage of time (rain, washing etc.) Worn chains act a bit like mechanical AIDS, they pass on wear to everything they engage with. So cassettes wear out more rapidly and so do front rings. Changing chains when they get to 0.5% is a recipe for overall economy and smooth shifting.
2) Buying a new chain and it then being the correct length for your bike is lucky. If it is too short and you are on a full suspension bike, you can rip off or at least bend your mech. If it is too long, the chain can whip up and down and come off.
3) Even if the chain is the correct length (see YouTube for advice), when travelling over rough ground the chain can still whip up and down to a degree. If your mech has a small lever on it (1"-1.5"), then that is the lever that actuates the friction clutch. It reduces chain whip substantially. Try moving the lower mech arm forward, with and without the clutch engaged. Note: not all mechs have a clutch.
4) If your chain has done a lot of miles, then be prepared for having to replace the cassette and the front ring as well! Does the new chain (length adjusted) run well with the cassette and front ring? Then you are probably OK.
PS: Chains should not keep coming off and they should never just break. Either is a symptom of something wrong. Wear and tear, mal-adjustment of something in the drivetrain, poor shiftng technique, or foreign body intrusion (we all get that!)