Hibernate CacheException -
i try create , fill database of hibernate.
my hibernate-config:
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url"> jdbc:h2:c:\users\data\datastore </property> <property name="connection.username">admin</property> <property name="connection.password">admin</property> <property name="connection.driver_class">org.h2.driver</property> <property name="dialect">org.hibernate.dialect.h2dialect</property> <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.ehcacheregionfactory</property> <property name="current_session_context_class">thread</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.jdbctransactionfactory</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="de.model.player" /> <mapping class="de.model.team" /> <mapping class="de.model.goal" /> <mapping class="de.model.match" /> </session-factory>
my data-access-object:
public class dao { /** * */ public void clean() { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.createquery("delete player").executeupdate(); session.createquery("delete team").executeupdate(); session.createquery("delete goal").executeupdate(); session.createquery("delete match").executeupdate(); session.flush(); session.clear(); transaction.commit(); } /** * * @param player */ public void insertplayer(player player) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.save(player); transaction.commit(); } /** * * @param team */ public void insertteam(team team) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.save(team); transaction.commit(); } /** * * @param goal */ public void insertgoal(goal goal) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.save(goal); transaction.commit(); } /** * * @param match */ public void insertmatch(match match) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.save(match); transaction.commit(); } /** * * @param player */ public void updateplayer(player player) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.update(player); transaction.commit(); } /** * * @param team */ public void updateteam(team team) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.update(team); transaction.commit(); } /** * * @param goal */ public void updategoal(goal goal) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.update(goal); transaction.commit(); } /** * * @param match */ public void updatematch(match match) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.update(match); transaction.commit(); } /** * * @param player */ public void deleteplayer(player player) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.delete(player); transaction.commit(); } /** * * @param team */ public void deleteteam(team team) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.delete(team); transaction.commit(); } /** * * @param goal */ public void deletegoal(goal goal) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.delete(goal); transaction.commit(); } /** * * @param match */ public void deletematch(match match) { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); transaction transaction = session.begintransaction(); session.delete(match); transaction.commit(); } }
my main:
public class main { /** * */ private static logger log = logger.getlogger(main.class); /** * */ private static dao dao = new dao(); /** * */ private static xmlreader reader = new xmlreader(); /** * * @param args */ public static void main(string[] args) { try { document allteams = reader.parsefile("src/main/resources/teams.xml"); document allmatches = reader.parsefile("src/main/resources/matches.xml"); list<element> teams = allteams.getrootelement().getchildren("team"); for(element team: teams) { team newteam = new team(); newteam.setid(integer.parseint(team.getchild("teamid").getvalue())); newteam.setname(team.getchild("teamname").getvalue()); newteam.seticonurl(team.getchild("teamiconurl").getvalue()); newteam.setstadion(team.getchild("stadion").getvalue()); newteam.setplayer(new hashset<player>()); list<element> players = team.getchildren("player"); for(element player: players) { player newplayer = new player(); newplayer.setname(player.getvalue()); newplayer.setteam(newteam); newteam.getplayer().add(newplayer); } dao.insertteam(newteam); for(player player: newteam.getplayer()) { dao.insertplayer(player); } } // dao.clean(); } catch(runtimeexception e) { try { session session = dbsessionhandler.getsessionfactoryinstance().getcurrentsession(); if(session.gettransaction().isactive()) { session.gettransaction().rollback(); } } catch(hibernateexception e1) { log.error("error rolling transaction"); } throw e; } catch (jdomexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
when executing main following error:
exception in thread "main" org.hibernate.cache.cacheexception: net.sf.ehcache.cacheexception: unnamed cachemanager exists in same vm. please provide unique names each cachemanager in config or 1 of following: 1. use 1 of cachemanager.create() static factory methods reuse same cachemanager same name or create 1 if necessary 2. shutdown earlier cachemanager before creating new 1 same name.
how have solve problem? did first hibernate-try of tutorial. in tutorial worked ^^
try change this:
<property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.ehcacheregionfactory </property>
to
<property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.singletonehcacheregionfactory </property>
Comments
Post a Comment