它说,我似乎无法让hibernate使用c3p0进行连接池
12:30:35,038 INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
12:30:35,038 INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
Run Code Online (Sandbox Code Playgroud)
Hibernate配置:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/xxx?autoReconnect=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">xxxx</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.max_statements">200</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud) 我需要在jar中打包配置文件.配置文件位于jar文件的根目录下.但是我收到以下错误:
引起:java.lang.IllegalArgumentException:URI在java.io.File中不是分层的.(未知来源)
File url = new File(MyClass.class.getClassLoader().getResource("my.conf").toURI());
Run Code Online (Sandbox Code Playgroud) 我正在使用Hibernate,试图模拟2个并发更新到数据库中的同一行.
编辑:我移动em1.getTransaction().提交在em1.flush()之后; 我没有得到任何StaleObjectException,这两个事务成功提交.
Session em1=Manager.sessionFactory.openSession();
Session em2=Manager.sessionFactory.openSession();
em1.getTransaction().begin();
em2.getTransaction().begin();
UserAccount c1 = (UserAccount)em1.get( UserAccount.class, "root" );
UserAccount c2 = (UserAccount)em2.get( UserAccount.class, "root" );
c1.setBalance( c1.getBalance() -1 );
em1.flush();
System.out.println("balance1 is "+c2.getBalance());
c2.setBalance( c2.getBalance() -1 );
em2.flush(); // fail
em1.getTransaction().commit();
em2.getTransaction().commit();
System.out.println("balance2 is "+c2.getBalance());
Run Code Online (Sandbox Code Playgroud)
我得到以下例外em2.flush().为什么?
2009-12-23 21:48:37,648 WARN JDBCExceptionReporter:100 - SQL Error: 1205, SQLState: 41000
2009-12-23 21:48:37,649 ERROR JDBCExceptionReporter:101 - Lock wait timeout exceeded; try restarting transaction
2009-12-23 21:48:37,650 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session …Run Code Online (Sandbox Code Playgroud) find ./dir -type f -iname "*.t[argz]*[bz2]" -print | xargs mv --target-directory=dir
似乎在名称中包含空格的文件上失败.怎么改进呢?还是另类?
谢谢你的回答:我的mv不支持--null或-0,我正在使用cygwin:
$ mv --help Usage: mv [OPTION]... [-T] SOURCE DEST or: mv [OPTION]... SOURCE... DIRECTORY or: mv [OPTION]... -t DIRECTORY SOURCE... Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. Mandatory arguments t .
我有一个servlet为用户做一些工作,然后减少用户的信用.当我实时查看数据库中用户的信用时,如果来自同一用户的并发请求很多,则由于并发控制而导致信用额被错误地扣除.T假设我有一个服务器,并且使用的数据库管理是休眠.我正在使用事务控制来跨越整个请求,请参阅代码以获取详细信息.我有几个问题:
为什么当面对来自同一用户的许多并发请求时,db中的信用计数器会跳到各处?为什么我的交易控制不起作用?
如果我在检索用户帐户后修改了基础数据,然后尝试更新它,为什么我没有得到任何数据HibernateException(eg.StaleObjectException)呢?
我有整个用户请求的事务跨度,有更好的方法吗?请批评.如果您觉得我做错了,请随意重写示例代码结构.
Main servlet class:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
Manager.beginTransaction();
cmdDowork(request, response);
Manager.commitTransaction();
}catch(Exception exp){
Manager.rollbackTransaction();
exp.printStackTrace();
}
finally{
Manager.closeSession();
}
}
public void cmdDowork(){
try{
UserAccount userAccount = lazyGetUserAccount(request.getParameter("userName"));
doWorkForUser(userAccount);//time and resource consuming process
if(userAccount!=null) {
decUserAccountQuota(userAccount);
}
}catch (HibernateException e){
e.printStackTrace();
}
}
public static UserAccount lazyGetUserAccount(String userName) {
UserAccount userAccount = Manager.getUserAccount(userName);
if(userAccount == null){
userAccount = new UserAccount(userName);
userAccount.setReserve(DEFAULT_USER_QUOTA);
userAccount.setBalance(DEFAULT_USER_QUOTA);
Manager.saveUserAccount(userAccount);
}
return userAccount;
} … 假设我有一个文件可能由一个线程/进程Writer写入并由另一个线程/进程Reader读取.Writer每隔x个时间间隔更新一次文件,Reader会每隔y个时间间隔读取一次文件,如果它们碰巧同时读写文件,会有任何问题吗?在写完成之前,读取块会被阻塞吗?或读取失败?反之亦然?这里的最佳做法是什么?