Java 与数据库的同步时间
我有一个拍卖系统应用程序,在线程中连接数据库,检查拍卖是否结束。如果我没有任何Thread.sleep,那么它会显示以下错误:java.sql.SQLException:Listener拒绝连接,并出现以下错误:ORA-12519,TNS:未找到合适的服务处理程序。 如何在不使用Thread.sleep的情况下检查拍卖是否完成Java 与数据库的同步时间,java,sql,multithreading,jdbc,Java,Sql,Multithreading,Jdbc,我有一个拍卖系统应用程序,在线程中连接数据库,检查拍卖是否结束。如果我没有任何Thread.sleep,那么它会显示以下错误:java.sql.SQLException:Listener拒绝连接,并出现以下错误:ORA-12519,TNS:未找到合适的服务处理程序。 如何在不使用Thread.sleep的情况下检查拍卖是否完成 private class AuctionRunnable implements Runnable { private int[] items;
private class AuctionRunnable implements Runnable {
private int[] items;
private String baseUser;
public AuctionRunnable(int[] items,String baseUser)
{
this.items=items;
this.baseUser = baseUser;
}
public void run()
{
for(int i=0;i<items.length;i++)
{
while (checkTime(baseUser)) // sprawdza czy aukcja zakonczona
{
available = true;
//Thread.sleep(1000);
}
available = false;
// Found winner
}
}
}
public boolean checkTime(String user) {
String lsql = "SELECT END_DATE FROM AUCTIONS WHERE SYSDATE<END_DATE";
DBWork db = new DBWork(user);
ResultSet lres;
try {
lres = db.RunQuery(lsql);
while (lres.next()) {
try {
db.terminate();
} catch (Exception ex) {
Logger.getLogger(TwoServers2.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
我不确定您在DBWork db=new DBWorkuser;,中到底做了什么;,但我假设您正在打开与数据库的连接。数据库在特定时间只允许一定数量的连接。每次进行查询时,都会与数据库建立新连接。终止与数据库的连接取决于while lres.next。因此,如果结果集没有返回任何内容,则您永远不会为上一个查询终止与数据库的连接。在调用足够多的checkTimeString用户后,可用的连接总数将被用完。因此,睡眠会减慢打开DB连接的速度 与其每次查询数据库时都建立新连接,不如将连接对象移到while循环之外,首先初始化到数据库的连接,然后对该连接对象进行查询。像这样的
import java.sql.* ;
public class SqlSpeed{
private String threadName;
private Thread t;
private static Connection connection;
public SqlSpeed( String name){
this.threadName = name;
this.connection = null;
}
/*******************************************************************
/--Looks for the mariaDB driver
/*******************************************************************/
private void findDBdriver(){
try {
Class.forName("org.mariadb.jdbc.Driver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
/*******************************************************************
/--Initilizes the connection Object
/*******************************************************************/
public Connection initDB(){
findDBdriver();
Connection connection = null;
try {
String URL = "jdbc:mysql://localhost:3306/database";
String USER = "----";
String PASS = "-----";
connection = DriverManager.getConnection(URL, USER, PASS);
}
catch (SQLException ex){
System.out.println("Error: No Connection to DB can be made !");
System.exit(1);
}
this.connection = connection;
return connection;
}
public void run(){
//use the connection object for your SQL quierres
}
public static void main(String[] args){
SqlSpeed test = new SqlSpeed("test");
test.initDB();
}
}
**只有在所有线程都使用相同的sql帐户时,您才需要静态连接。如果删除睡眠,您查询DB的速度会太快。您能显示checkTime方法的代码吗?也许你可以使用连接池?